Skip to content

Commit

Permalink
Merge pull request #4 from davidfischer/davidfischer/use-cairosvg-to-…
Browse files Browse the repository at this point in the history
…pdfs

Use CairoSVG to generate PDFs
  • Loading branch information
davidfischer authored Jan 18, 2022
2 parents 92558c7 + fb738a2 commit db479c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ It requires Python 3.6+ and has a few dependencies.


pip install -r requirements.txt # Install dependencies
python label-generator.py # Creates files in output/
python label-generator.py # Creates SVG & PDF files in output/

By default, this will create one or more SVG files.
These files are vector image files that can be customized further
or printed using most modern browsers and many other tools.
By default, this will create SVG & PDF files.
The SVG files are vector image files that can be customized further.
The PDF files are ready to print.

The SVGs use the free fonts [EB Garamond][garamond] bold and [Source Sans Pro][source-sans] regular.

Expand All @@ -41,16 +41,6 @@ The SVGs use the free fonts [EB Garamond][garamond] bold and [Source Sans Pro][s
[source-sans]: https://fonts.google.com/specimen/Source+Sans+Pro


### Tips for printing

The output SVGs are precisely sized for a sheet of paper (US Letter by default).
Make sure while printing in your browser or otherwise to set the margins to None.

<img src="readme-img/browser-printing.png">

You can also "print" to a PDF.


### Customizing

A lot of features can be customized by changing constants at the top of `label-generator.py`.
Expand All @@ -66,6 +56,19 @@ You can change how the labels are actually displayed and rendered by customizing
If you change the fonts, you may also need to resize things to fit.


### Tips for printing SVGs

If you're just using the default PDFs, you probably won't need this.
However, if you are customizing the SVGs and printing them, this section is for you.

The output SVGs are precisely sized for a sheet of paper (US Letter by default).
Make sure while printing in your browser or otherwise to set the margins to None.

<img src="readme-img/browser-printing.png">

You can also "print" to a PDF.


## License

The code is available at [GitHub][home] under the [MIT license][license].
Expand Down
28 changes: 19 additions & 9 deletions label-generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
import os
import subprocess
from datetime import datetime
from pathlib import Path

import cairosvg
import jinja2
import requests


BASE_DIR = os.path.abspath(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = Path(os.path.abspath(os.path.dirname(__file__)))

ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.join(BASE_DIR, "templates")),
loader=jinja2.FileSystemLoader(BASE_DIR / "templates"),
autoescape=jinja2.select_autoescape(["html", "xml"]),
)

Expand Down Expand Up @@ -93,7 +95,7 @@

class LabelGenerator:

DEFAULT_OUTPUT_DIR = os.path.join(BASE_DIR, "output")
DEFAULT_OUTPUT_DIR = BASE_DIR / "output"

COLS = 4
ROWS = 15
Expand All @@ -118,7 +120,7 @@ def __init__(self, paper_size=None, output_dir=None):
self.delta_x = (self.width - (2 * self.MARGIN)) / self.COLS
self.delta_y = (self.height - (2 * self.MARGIN)) / self.ROWS

self.output_dir = output_dir or DEFAULT_OUTPUT_DIR
self.output_dir = Path(output_dir or DEFAULT_OUTPUT_DIR)

# Set data from scryfall
self.set_data = self.get_set_data()
Expand All @@ -140,13 +142,21 @@ def generate_labels(self):
WIDTH=self.width,
HEIGHT=self.height,
)
outfile = os.path.join(
self.output_dir, f"labels-{self.paper_size}-{page:02}.svg"
outfile_svg = self.output_dir / f"labels-{self.paper_size}-{page:02}.svg"
outfile_pdf = str(
self.output_dir / f"labels-{self.paper_size}-{page:02}.pdf"
)
print(f"Writing {outfile}...")
with open(outfile, "w") as fd:

print(f"Writing {outfile_svg}...")
with open(outfile_svg, "w") as fd:
fd.write(output)

print(f"Writing {outfile_pdf}...")
with open(outfile_svg, "rb") as fd:
cairosvg.svg2pdf(
file_obj=fd, write_to=outfile_pdf,
)

page += 1

def get_set_data(self):
Expand Down Expand Up @@ -259,7 +269,7 @@ def create_vertical_cutting_guides(self):
parser.add_argument(
"--output-dir",
default=LabelGenerator.DEFAULT_OUTPUT_DIR,
help='Output labels to this directory',
help="Output labels to this directory",
)
parser.add_argument(
"--paper-size",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Jinja2==2.11.3
requests==2.22.0
CairoSVG==2.5.2

# For development only
black==19.10b0

0 comments on commit db479c1

Please sign in to comment.