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