Last active
October 24, 2023 16:23
-
-
Save bitsandbooks/687673180903cccdd6dec4ca78bb4272 to your computer and use it in GitHub Desktop.
Flash Book Generator
This file contains hidden or 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
#!/usr/bin/env python3 | |
# This script generates a simple PDF "flash book" (such as one through which | |
# you might browse at a tattoo parlor) from a folder of SVG files. Each item is | |
# on a separate page, with its file name written beneath. This makes it easy to | |
# re-create the flash book whenever SVGs are added, deleted, or renamed. | |
# | |
# NOTES AND RECOMMENDATIONS: | |
# | |
# - `flashbook.py` must be executable. If it is not, you may need to use | |
# Python to run the script with: `python3 flashbook.py`. | |
# - The `requirements.txt` file is a list of the Python libraries you will need | |
# to run the script. You can use venv to create a virtual environment, and | |
# then install these with `pip install -r requirements.txt`. | |
# - For best results when printing the PDF, use double-sided printing (if you | |
# have it), and most PDF readers (such as Acrobat or Preview.app) will also | |
# let you print multiple pages per sheet of paper. These can help cut waste. | |
# | |
# INSTRUCTIONS: | |
# | |
# You may either run `flashbook.py` from any terminal, in which case it will | |
# prompt you, or you may script its usage with | |
# | |
# `flashbook.py input_folder output_file.pdf` | |
# | |
# COPYRIGHT AND LICENSE: | |
# | |
# Copyright © 2023 by Rob Dumas. | |
# | |
# This program is licensed under the GNU General Public License, version 3. | |
# Your use of it constitutes acceptance of the terms of said license. For the | |
# full text, visit https://www.gnu.org/licenses/gpl-3.0.en.html | |
import os, sys | |
from reportlab.graphics import renderPDF | |
from reportlab.pdfgen import canvas | |
from reportlab.lib.pagesizes import letter | |
from svglib.svglib import svg2rlg | |
def create_pdf_from_svgs(folder, output_pdf): | |
# Scan the folder for SVGs | |
svg_files = [f for f in os.listdir(folder) if f.endswith('.svg')] | |
svg_files.sort() # Sort alphabetically | |
# Prepare the PDF canvas | |
pdf_file = os.path.join(folder, output_pdf) | |
c = canvas.Canvas(pdf_file, pagesize=letter) | |
c.setAuthor("Rob Dumas") | |
c.setCreator("Python 3 and ReportLab") | |
c.setSubject("Flash book generator for SVGs") | |
c.setTitle("Elk Lab SVG Flash Book") | |
width, height = letter # 612.0 x 792.0 | |
max_dim = round(width * 0.75, 0) | |
for index, svg_file in enumerate(svg_files, start=1): | |
# Convert SVG to a format that reportlab can understand | |
drawing = svg2rlg(os.path.join(folder, svg_file)) | |
scaleFactorX = max_dim / drawing.width | |
scaleFactorY = max_dim / drawing.height | |
scaleFactor = min(scaleFactorX, scaleFactorY) | |
drawing.width, drawing.height = drawing.minWidth() * scaleFactor, drawing.height * scaleFactor | |
drawing.scale(scaleFactor, scaleFactor) | |
# Position the drawing in the middle of the page | |
drawing_x_pos = (width - drawing.width) / 2 | |
drawing_y_pos = (height - drawing.height) / 2 | |
# Add filename to bottom of page | |
c.setFont("Helvetica", 30) | |
c.drawCentredString(width / 2, 50, svg_file) | |
# Draw the SVG | |
renderPDF.draw(drawing, c, drawing_x_pos, drawing_y_pos) | |
# Move to the next page | |
c.showPage() | |
c.save() | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print("Scriptable usage: flashbook.py input_folder output_file.pdf") | |
input_folder = input("Enter the path to the folder containing SVG files: ") | |
output_pdf = input("Enter the PDF file to generate: ") | |
create_pdf_from_svgs(input_folder, output_pdf) | |
print(f"PDF generated and saved as '{output_pdf}'") | |
else: | |
input_folder = sys.argv[1] | |
output_pdf = sys.argv[2] | |
create_pdf_from_svgs(input_folder, output_pdf) | |
print(f"PDF generated and saved as '{output_pdf}'") |
This file contains hidden or 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
wheel | |
setuptools | |
reportlab | |
svglib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment