通过PHP将HTML转换为PDF(为html2pdf安装字体) [英] Converting HTML to PDF via PHP (install font for html2pdf)

查看:1123
本文介绍了通过PHP将HTML转换为PDF(为html2pdf安装字体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正尝试通过PHP脚本将HTML标记(从XML feed)转换为PDF。



阅读其他答案,最好的免费方法这样做似乎是使用 html2pdf
$ b

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

我遇到的问题是我的 $ htmlContent 包含css: FONT-FAMILY:Times New Roman,serif在不同的地方,我的脚本给出错误:

TCPDF错误:无法包含字体定义文件:times new roman



我GOOGLE了,唯一的文档是这样的:
http://wiki.spipu.net/doku.php?id=html2pdf:en:v4:font



这反过来导致你:
http://www.tcpdf.org/fonts.php



虽然我迷路了,但第二个链接指出times / Times New Roman是一个核心的PDF字体...我尝试了各种各样的东西,得到相同的错误。



我实际上需要写什么来添加字体,或者我怎样才能将所有的FONT-FAMILY类从 $ htmlContent (我甚至不需要它在任何特定的字体,只是一个工程)。

解决方案

是的,你应该使用DOMPDF其他图书馆。有很多字体可用。您可以在dompdf / lib / fonts / dompdf_font_family_cache.dist.php处看到并根据您的要求进行选择。要更改字体,您需要在文件路径dompdf / dompdf_config.inc.php中更改def(DOMPDF_DEFAULT_FONT,serif):

DOMPDF的最新版本来自: https://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.tar.gz&can=2&q=



请参阅下面的示例:

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

$ fileName =invoice.pdf;
$ dompdf-> stream($ fileName); //下载PDF

//将输出作为字符串并输入到某些文件
$ output = $ dompdf-> ;输出();
file_put_contents($ fileName,$ output);


I'm currently attempting to convert HTML markup (from an xml feed) into a PDF dynamically via a PHP script.

From reading other answers the best free way of doing this seemed to be to use 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:

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

Which in turn leads you to: 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.

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).

解决方案

Yes, you should use DOMPDF instead of other libraries. 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. For change the font you need to change the def("DOMPDF_DEFAULT_FONT", "serif") at file path "dompdf/dompdf_config.inc.php":

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=

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);

这篇关于通过PHP将HTML转换为PDF(为html2pdf安装字体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆