【问题标题】:WhtmlToPdf doesn't apply fonts to PDFWkhtmlToPdf 不将字体应用于 PDF
【发布时间】:2016-03-16 13:41:43
【问题描述】:

我目前正在开发一个文档生成器 symfony 2 包,它允许生成 HTML 和 PDF 文档(例如发票)。

当我在浏览器中将文档生成为 HTML 页面时,一切都很好,但是当我生成 PDF 文件时,我的文档没有应用任何字体。

我对这两种情况都使用相同的模板。这是我的pdf生成方法:

private function generatePdfWithoutMerge($html)
{
    $pdfService = $this->get('knp_snappy.pdf');

    return $pdfService->getOutputFromHtml($html);
}

$html 变量是从渲染的树枝模板生成的,如前所述,如果我在浏览器中将其显示为 HTML,我没有问题。

少文件:

@base-url: 'url-to-bucket';
@dark-grey: #555559;
@dark-pink: #970047;

@font-face {
    font-family: paralucent_demi_boldregular;
    src: url('@{base-url}/paradb__-webfont.eot');
    src: url('@{base-url}/paradb__-webfont.eot?#iefix') format('embedded-opentype'),
    url('@{base-url}/paradb__-webfont.woff') format('woff'),
    url('@{base-url}/paradb__-webfont.ttf') format('truetype'),
    url('@{base-url}/paradb__-webfont.svg#paralucent_demi_boldregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: paralucent_lightregular;
    src: url('@{base-url}/paral___-webfont.eot');
    src: url('@{base-url}/paral___-webfont.eot?#iefix') format('embedded-opentype'),
    url('@{base-url}/paral___-webfont.woff') format('woff'),
    url('@{base-url}/paral___-webfont.ttf') format('truetype'),
    url('@{base-url}/paral___-webfont.svg#paralucent_lightregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: paralucent_thinregular;
    src: url('@{base-url}/parat___-webfont.eot');
    src: url('@{base-url}/parat___-webfont.eot?#iefix') format('embedded-opentype'),
    url('@{base-url}/parat___-webfont.woff') format('woff'),
    url('@{base-url}/parat___-webfont.ttf') format('truetype'),
    url('@{base-url}/parat___-webfont.svg#paralucent_thinregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: paralucent_mediumregular;
    src: url('@{base-url}/param___-webfont.eot');
    src: url('@{base-url}/param___-webfont.eot?#iefix') format('embedded-opentype'),
    url('@{base-url}/param___-webfont.woff') format('woff'),
    url('@{base-url}/param___-webfont.ttf') format('truetype'),
    url('@{base-url}/param___-webfont.svg#paralucent_mediumregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

body {
    color: @dark-grey;
    font-family: paralucent_lightregular, Helvetica, Arial, sans-serif;
    font-size: 1.2em;
    line-height: 0.9em;
}

.thin {
    font-family: paralucent_thinregular, Helvetica, Arial, sans-serif;
}

.bold {
    font-family: paralucent_mediumregular, Helvetica, Arial, sans-serif;
}

.demibold {
    font-family: paralucent_demi_boldregular, Helvetica, Arial, sans-serif;
}

.price {
    font-family: Arial, sans-serif;
    font-size: 0.8em;
}

.separator_unbreak_wrapper {

}

.separator_unbreak {
    page-break-inside: avoid !important;
}

.city_format {
    font-size: 0.75em;

}

table tr th {
    border-bottom: 1px solid @dark-pink;
    font-weight: normal;
    font-style: normal;
}

【问题讨论】:

  • 你用的是什么字体?
  • 我的字体是 .eot、.woff、.ttf 和 .svg 文件,托管在 S3 存储桶上,CORS 设置如下:localhostGET *
  • 我不知道这个库,但如果它足够聪明,可以在 HTML 文档中嵌入引用的font-face 字体,我会印象深刻......也许它们需要手动添加?不过,我真的对这个产品一无所知。
  • 在您的示例中,此人使用 GoogleFont 链接。没关系,但我有一个在上面找不到的特定字体:Paralucent。如前所述,我有字体文件。我将在 OP 中发布我的 LESS 文件。
  • wkhtmltopdf 对任何外部资源都有很大的麻烦,即使是通过其内部浏览器解析的样式表也是如此。你将无法实现你想要的。你必须使用更复杂的东西。

标签: symfony pdf fonts


【解决方案1】:

感谢 DevDonkey 评论:

我尝试使用从公共父模板和公共 LESS 文件扩展而来的子模板的良好文件架构,但 Wkhtmltopdf 在这种复杂架构方面存在一些问题。 我必须将所有内容收集到一个 HTML 文件中才能使其正常工作。

非常感谢大家的帮助。

【讨论】:

  • 这并不意味着 Wkhtmltopdf 必须解释 LESS 文件,对吧?它接收到正常的 CSS 来解析?
  • 不,因为它是一个使用assetic的twig模板,render()调用会将LESS文件处理成CSS文件,并将所有twig文件处理成HTML文件。顺便说一句,twig 包含/扩展就像一个魅力,因此您可以使用
猜你喜欢
  • 2011-10-06
  • 2013-01-06
  • 1970-01-01
  • 2017-05-08
  • 2013-07-23
  • 2013-03-22
  • 1970-01-01
  • 2017-12-24
  • 2012-01-24
相关资源
最近更新 更多