xhtml2pdf不嵌入Helvetica字体

0 投票
1 回答
1619 浏览
提问于 2025-04-18 16:32

我正在用xhtml2pdf和Django创建一个PDF文件。我把这个PDF发送去打印,但他们说有些字体没有嵌入。我有一个Helvetica字体,但我在PDF中并没有使用Helvetica。

这里有一张PDF属性的截图

在这里输入图片描述

如你所见,Guilles'ComicFont和TF2Secondary字体是正确嵌入的,但Helvetica却没有。

这是我生成PDF的视图代码:

def generate_pdf(request, book, order):
    try:
        book = Book.objects.get(pk=int(book))
        order = Order.objects.get(identificador=order, cuento=book)
    except ObjectDoesNotExist:
        raise Http404


    data = {}
    data = ast.literal_eval(order.datos_variables)

    data['order'] = order

    template = get_template(order.book.plantilla_html)
    html  = template.render(Context(data))

    url = '/Users/vergere/dev/media/pdfs/%s.pdf' % order.identificador

    fichero = open(url, "w+b")
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=fichero, encoding='utf-8')

    fichero.seek(0)
    fichero.close()


    order.url_pdf = '%s.pdf' % order.identificador
    order.contador_libro = numero
    order.codigo_libro = country_code
    order.save()

    return HttpResponseRedirect(reverse('order_single', kwargs={'pedido': order.pk}))

这是我的HTML代码:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Background</title>
        <style>
            @page {
                background-image: url("cuento1/img/portada.jpg");
                size: 213mm 216mm;
            }

            @font-face {
              font-family: Helvetica;
              src: url(pdf_generator/helvetica.ttf);
            }
            @font-face {
                font-family: 'Gilles';
                src: url(pdf_generator/cuento1/gilles/gilles.ttf);
                font-weight: normal;
                font-style: normal;
            }
            @font-face {
                font-family: 'Secondary';
                src: url(pdf_generator/cuento1/tf2_secondary/tf2_secondary.ttf);
                font-weight: normal;
                font-style: normal;
            }
            * {
                box-shadow:none !important;
                margin: 0;
                padding: 0;
                text-shadow: none !important;
                font-family: 'Gilles';
            }
            body{
                font-family: 'Gilles';
            }
            p {
                color: #464439;
                display: block;
                font-weight: normal;
                position: absolute;
            }
            .page-1 p, .page-2 p{
                font-family: 'Secondary';
                font-size: 40px;
                line-height: 1.3em;
                position: absolute;
            }
        </style>
    </head>
    <body>
        <pdf:nextpage name="p1" />
        <pdf:nextpage name="p2" />
        <div class="page-6 page-dedicatoria">
            {{order.dedicatoria}} <br /> {{order.de}}
        </div>
        <p>&nbsp;</p>
    </body>
</html>

有人知道为什么会使用Helvetica吗?或者有没有办法嵌入Helvetica字体?我试过用"@font-face",但没有成功。

1 个回答

0

Helvetica 是一种标准字体,几乎所有的 PDF 阅读器都会自带这个字体。所以在制作 PDF 时,不需要把它嵌入文件里。

如果想要解决这个问题,可以考虑用其他无衬线字体来代替 Helvetica。比如在 Windows 系统上可以用 Arial,而在 OSX 系统上可以用 Helvetica Neue 或 Avenir。这些字体看起来和 Helvetica 很像,但它们不是标准的 PDF 字体。

在你的样式表中,可以为所有元素指定一种新的字体;

* {
    font-family: Avenir;
}

撰写回答