[feat] add action to generate pdf of textbook #6
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Generate PDF for Textbook | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
jobs: | |
build_and_generate_pdf: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Ruby | |
uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: 3.1 | |
bundler-cache: true | |
- name: Install Jekyll dependencies | |
run: | | |
bundle install | |
- name: Install Google Chrome | |
run: | | |
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb | |
sudo apt-get update | |
sudo apt-get install -y ./google-chrome-stable_current_amd64.deb | |
rm google-chrome-stable_current_amd64.deb | |
- name: Install PDF tools (poppler for pdfunite) | |
run: | | |
sudo apt-get install -y poppler-utils | |
- name: Convert HTML to PDF and merge them | |
run: | | |
site_folder="_site" | |
output_folder="pdf_output" | |
combined_pdf="textbook_full.pdf" | |
mkdir -p "$output_folder" | |
file_list=( | |
"index.html" | |
"principles/index.html" | |
"principles/principles.html" | |
) | |
for html_file in "${file_list[@]}"; do | |
full_path="$site_folder/$html_file" | |
output_pdf="$output_folder/$(basename "$html_file" .html).pdf" | |
echo "file://$PWD/$full_path" | |
if [ -f "$full_path" ]; then | |
echo "Generating PDF for $html_file" | |
google-chrome --headless --disable-gpu --disable-software-rasterizer --no-sandbox --no-pdf-header-footer --print-to-pdf="$output_pdf" "file://$PWD/$full_path" | |
else | |
echo "Error: File $full_path not found!" >&2 | |
exit 1 | |
fi | |
done | |
pdfunite "$output_folder"/*.pdf "$PWD/$combined_pdf" | |
- name: Upload PDF to site | |
uses: actions/upload-artifact@v3 | |
with: | |
name: textbook-full | |
path: textbook_full.pdf | |
- name: Commit and push changes only on push | |
if: github.event_name == 'push' | |
run: | | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "GitHub Actions" | |
git add textbook_full.pdf | |
git commit -m "Update full PDF of textbook on site" | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |