python - Lulu 希望在我的 reportlab PDF 中嵌入字体以出版一本书

标签 python pdf fonts reportlab

我收集了一堆domino games并将它们发布为我使用 reportlab 生成的 PDF。现在,我正在考虑出版一本纸质书要花多少钱,但 Lulu 提示我的 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.

我使用了 Acrobat Reader 中内置的字体,例如 Helvetica,因此我认为它们可以在任何地方使用。

这是一个让露露提示的简单例子:

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()

如何嵌入字体以便 Lulu 接受我的 PDF?

最佳答案

reportlab FAQ描述了对我不起作用的内置字体,但它也解释了如何嵌入其他字体。我尝试了 reportlab 中包含的两种字体,Lulu 接受了 PDF。

为了有两种以上的字体可供选择,我从 Google Fonts 下载了一些这里有一个很好的搜索功能,可以推荐字体配对。

这是使用我下载的两种字体的最终代码:

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()

关于python - Lulu 希望在我的 reportlab PDF 中嵌入字体以出版一本书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67140620/

相关文章:

python - 在 Python 中读取 CSV 文件并将数据传递给 Jinja 模板

python - 类型错误:无法连接 'str' 和 'float' 对象

CSS 边距和填充不起作用

java - 验证 PDF 和 Excel 文件类型的模式

android - 特定字体的文本从前面被截断

python - MySQL Encoding 4 byte in 3 byte utf-8 - 不正确的字符串值

c# - 打开 PDF 文件时出现“PDFsharp 无法处理 Acrobat 6 引入的此 PDF 功能”错误

uikit - 在 iOS 上,我可以访问系统提供的字体的 TTF 文件吗?

java - 如何检查 java.awt 中有哪些字体可用?

python - 基于 raw_input 的 read_csv 数据框中的列位置的变量替换