简体   繁体   English

通过PHP将HTML转换为PDF(html2pdf的安装字体)

[英]Converting HTML to PDF via PHP (install font for html2pdf)

I'm currently attempting to convert HTML markup (from an xml feed) into a PDF dynamically via a PHP script. 我目前正在尝试通过PHP脚本将HTML标记(来自xml提要)动态转换为PDF。

From reading other answers the best free way of doing this seemed to be to use html2pdf . 通过阅读其他答案,做到这一点的最好的免费方法似乎是使用html2pdf

// HTML2PDF
require_once('/public_html/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','en');
$html2pdf->WriteHTML($htmlContent);
$html2pdf->Output('/public_html/wp-content/uploads/rns/html2pdf.pdf');

The problem I'm having is my $htmlContent contains the css: FONT-FAMILY: "Times New Roman","serif" in various places and my script gives the error: 我遇到的问题是我的$htmlContent包含css: FONT-FAMILY: "Times New Roman","serif" $htmlContent FONT-FAMILY: "Times New Roman","serif"在各个地方,并且我的脚本给出了错误:

TCPDF ERROR: Could not include font definition file: "times new roman"

I've googled and the only documentation is this: http://wiki.spipu.net/doku.php?id=html2pdf:en:v4:font 我已经用Google搜索过,唯一的文档是这样的: http : //wiki.spipu.net/doku.php?id= html2pdf:en:v4: font

Which in turn leads you to: http://www.tcpdf.org/fonts.php 依次将您引导至: http : //www.tcpdf.org/fonts.php

I'm lost though, the second link says that times/Times New Roman is a core PDF font... I've tried various things and get the same error. 不过,我迷路了,第二个链接说,Times / Times New Roman是一种核心PDF字体……我尝试了各种尝试,并得到了相同的错误。

What would I actually need to write to add the font, or alternatively, how could I strip all of the FONT-FAMILY classes out of the $htmlContent (I don't even need it in any particular font, just one that works). 我实际上需要写些什么来添加字体,或者如何将所有FONT-FAMILY类从$htmlContent (我什至不需要任何特定的字体,只需要一种有效的字体)。

Yes, you should use DOMPDF instead of other libraries. 是的,您应该使用DOMPDF而不是其他库。 There are lots of fonts available. 有很多可用的字体。 You can see at "dompdf/lib/fonts/dompdf_font_family_cache.dist.php" and choose according to your requirements. 您可以在“ dompdf / lib / fonts / dompdf_font_family_cache.dist.php”中查看并根据需要进行选择。 For change the font you need to change the def("DOMPDF_DEFAULT_FONT", "serif") at file path "dompdf/dompdf_config.inc.php": 要更改字体,您需要在文件路径“ dompdf / dompdf_config.inc.php”中更改def(“ DOMPDF_DEFAULT_FONT”,“ serif”):

You can download the latest version available of DOMPDF from: https://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.tar.gz&can=2&q= 您可以从以下网址下载DOMPDF的最新版本: https : //code.google.com/p/dompdf/downloads/detail? name = dompdf_0-6-0_beta3.tar.gz & can =2& q =

See the below example: 请参见以下示例:

require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($htmlContent);
$dompdf->render();                

$fileName = "invoice.pdf";
$dompdf->stream($fileName);//DOWNLOAD PDF 

//GET OUTPUT AS STRING AND PUT IN TO SOME FILE   
$output = $dompdf->output();
file_put_contents($fileName, $output);

Try to use DomPdf instead of html2pdf. 尝试使用DomPdf代替html2pdf。 It works great for me on creating invoices in PDF. 这对我创建PDF发票非常有用。 But some treatments (positioning and font sizes) in your CSS code are needed. 但是,您需要在CSS代码中进行一些处理(位置和字体大小)。 The screen content placed as it is didn't work as i expected. 屏幕内容按原样放置不正常。 https://github.com/dompdf/dompdf https://github.com/dompdf/dompdf

It's a bit of a hack, but I found the best way for me was to call capturejs using exec: 这有点hack,但是我发现对我来说最好的方法是使用exec调用capturejs:

$run = "capturejs -u ";
$run .= "https://www.example.com/";
$run .= " -p tlsv1 -V ";
$run .= "1024x768";
$run .= " -o 'myimage.png'";
exec($run);

https://github.com/superbrothers/capturejs https://github.com/superbrothers/capturejs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM