Lulu wants embedded fonts in my reportlab PDF to publish a book

Viewed 339

I've collected a bunch of domino games and published them as a PDF that I generate with reportlab. Now, I'm looking at how much it would cost to publish a paper book, but Lulu complains about my PDF:

Fonts: We found some fonts in your file that need to be embedded. Please review your PDF and make sure all Fonts are embedded.

I used the fonts that built into Acrobat Reader, such as Helvetica, so I assumed they would work anywhere.

Here's a simple example that makes Lulu complain:

from subprocess import run

from reportlab.lib import pagesizes
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph


def main():
    pdf_path = 'booklet.pdf'
    doc = SimpleDocTemplate(pdf_path,
                            pagesize=pagesizes.letter,
                            topMargin=0.625*inch,
                            bottomMargin=0.625*inch)
    styles = getSampleStyleSheet()
    normal_style: ParagraphStyle = styles['Normal']
    custom_style = ParagraphStyle('custom',
                                  parent=normal_style,
                                  fontSize=300,
                                  leading=360)
    story = []
    for i, text in enumerate('ABCDEFGH'):
        flowable = Paragraph(text, custom_style)
        story.append(flowable)

    doc.build(story)

    # run(["evince", pdf_path], check=True)  # launch PDF viewer


main()

How can I embed fonts so that Lulu will accept my PDF?

1 Answers

The reportlab FAQ describes the built-in fonts that didn't work for me, but it also explains how to embed other fonts. I tried the two fonts that are included with reportlab, and Lulu accepted the PDF.

In order to have more than two fonts to choose from, I downloaded some from Google Fonts where there's a nice search feature that recommends font pairings.

Here's the final code that uses two fonts I downloaded:

from subprocess import run

from reportlab.lib import pagesizes
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.pdfbase import pdfmetrics


def main():
    pdf_path = 'booklet.pdf'
    doc = SimpleDocTemplate(pdf_path,
                            pagesize=pagesizes.letter,
                            topMargin=0.625*inch,
                            bottomMargin=0.625*inch)
    fredoka_file = 'fonts/Fredoka_One/FredokaOne-Regular.ttf'
    raleway_file = 'fonts/Raleway/static/Raleway-Regular.ttf'
    pdfmetrics.registerFont(TTFont("Fredoka", fredoka_file))
    pdfmetrics.registerFont(TTFont("Raleway", raleway_file))
    styles = getSampleStyleSheet()
    normal_style: ParagraphStyle = styles['Normal']
    header_style = ParagraphStyle('Fredoka',
                                  parent=normal_style,
                                  fontName='Fredoka',
                                  fontSize=300,
                                  leading=360)
    body_style = ParagraphStyle('Raleway',
                                parent=normal_style,
                                fontName='Raleway',
                                fontSize=30,
                                leading=36)
    story = []
    for i, text in enumerate('ABCDEFGH'):
        if i % 2 == 0:
            custom_style = header_style
        else:
            custom_style = body_style
        flowable = Paragraph(text, custom_style)
        story.append(flowable)

    doc.build(story)

    # run(["evince", pdf_path], check=True)  # launch PDF viewer


main()
Related