0

我正在使用 Django 创建带有 xhtml2pdf 的 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”,但它不起作用。

4

1 回答 1

0

Helvetica 是每个 PDF 渲染器都必须具备的标准字体之一。因此,它不必嵌入。

一种可能的解决方案是使用另一种无衬线字体而不是 Helvetica。在 Windows 上,例如 Arial。在 OSX 上,例如 Helvetica Neue 或 Avenir。它们看起来很像 Helvetica,但不是标准的 PDF 字体。

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

* {
    font-family: Avenir;
}
于 2014-08-08T12:42:48.680 回答