使用 PHP 编写 PDF (PDFLib)
我目前正在编写一段代码,使用 http://www.pdflib.com/ 中的 PDFlib 在 PHP 中输出 pdf 文件。问题是所有 html 标签也写入输出文件中。如何才能取消所有这些标签?
这是我的示例代码。
$postVariable = $_POST;
$contentData = "";
foreach($postVariable as $key => $value){
if(is_array($key)){
foreach($key as $key1 => $value1){
$contentData.= $key1 .": ". $value1."<nextline>";
}
}else{
$contentData.= $key .": ". $value."<nextline>";
}
}
$testdata = nl2br($contentData);
$pdf = pdf_new();
// open a file
pdf_open_file($pdf, $_SERVER['DOCUMENT_ROOT']."cas".DIRECTORY_SEPARATOR."$filename.pdf");
// start a new page (A4)
pdf_begin_page($pdf, 595, 842);
// Locate Font Directory
$fontdir = "C:\WINDOWS\Fonts";
// Font Name Parameters
pdf_set_parameter($pdf, "FontOutline", "arialMyName=$fontdir\arial.ttf");
// Find Font
$arial = PDF_findfont($pdf,"arialMyName","host",0 );
// Set font size and font name
pdf_setfont($pdf, $arial, 10);
//$arial = pdf_findfont($pdf, "Arial", "host", 1);
//pdf_setfont($pdf, $arial, 10);
// print text
pdf_show_xy($pdf, "TO THE UNIT OWNER",50, 750);
pdf_show_xy($pdf, "Test ext", 50,730);
pdf_show_xy($pdf, "test test", 50,715);
pdf_show_xy($pdf, $contentData, 50,700);
// end page
pdf_end_page($pdf);
// close and save file
pdf_close($pdf);
示例输出为: 致单位业主 测试文本 测试测试 类型:公寓****var_name:****var_company:****var_date:****提交:保存并下载<
它忽略 html 标签并将其包含在内容中。
是否有其他方法可以使用我当前使用的库(PDFlib)将 HTML 打印为 PDF。
谢谢。
问候, 雷斯蒂
I'm currently writing a code to output a pdf file in PHP using PDFlib from http://www.pdflib.com/. The problem is all html tag is also written in the output file. How can be able to cancel out all those tags?
Here is my sample code.
$postVariable = $_POST;
$contentData = "";
foreach($postVariable as $key => $value){
if(is_array($key)){
foreach($key as $key1 => $value1){
$contentData.= $key1 .": ". $value1."<nextline>";
}
}else{
$contentData.= $key .": ". $value."<nextline>";
}
}
$testdata = nl2br($contentData);
$pdf = pdf_new();
// open a file
pdf_open_file($pdf, $_SERVER['DOCUMENT_ROOT']."cas".DIRECTORY_SEPARATOR."$filename.pdf");
// start a new page (A4)
pdf_begin_page($pdf, 595, 842);
// Locate Font Directory
$fontdir = "C:\WINDOWS\Fonts";
// Font Name Parameters
pdf_set_parameter($pdf, "FontOutline", "arialMyName=$fontdir\arial.ttf");
// Find Font
$arial = PDF_findfont($pdf,"arialMyName","host",0 );
// Set font size and font name
pdf_setfont($pdf, $arial, 10);
//$arial = pdf_findfont($pdf, "Arial", "host", 1);
//pdf_setfont($pdf, $arial, 10);
// print text
pdf_show_xy($pdf, "TO THE UNIT OWNER",50, 750);
pdf_show_xy($pdf, "Test ext", 50,730);
pdf_show_xy($pdf, "test test", 50,715);
pdf_show_xy($pdf, $contentData, 50,700);
// end page
pdf_end_page($pdf);
// close and save file
pdf_close($pdf);
and the sample output is:
TO THE UNIT OWNER
Test text
test test
type: Apartment****var_name: ****var_company: ****var_date: ****submit: Save and Download<
It disregards the html tags and it include it on the content.
Is there any other methods on how to print out HTML to PDF using the library that I'm currently using (PDFlib).
Thanks.
regards,
Resty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会推荐 TCPDF 库。它可以将您的 HTML 转换为 PDF 文件,包括 CSS 代码。它支持相当多的 HTML 标签,并且做得不错。然而,从个人经验来看,虽然它很有可能生成高质量的 PDF 文件,但结果并不总是 100% 符合预期。它可能需要一些调整和摆弄才能获得您想要的结果。
如果您只想删除 HTML 标签,我想您可能需要看看 strip_tags。但我想这并不是你真正想要的。
最后,还有一个非常酷的新东西 - 一个 PHP 扩展,它使用 WebKit HTML 和渲染引擎来生成 PDF。它被称为 libwkhtmltox扩展名,可以在这里找到。它产生了惊人的结果。例如,看看它从《纽约时报》主页制作的 PDF - 请参阅 ( http://www.2shared.com/document/kYuS_G7p/nytimes.html - 单击下面的“保存到我的电脑”)。该输出是在未指定任何其他选项的情况下生成的。非常出色。然而,您需要让它作为 PHP 扩展运行,这可能是一项艰巨的任务。所以我想说:现在坚持使用 TCPDF 库。
I would recommend the TCPDF library. It can convert your HTML to a PDF file, including CSS code. It has support for quite a lot of HTML tags, and does a decent job. HOWEVER, from personal experience, while it is very possible to generate high quality PDF files with it, the results are not always 100% as expected. It might need a bit of tweaking and fiddling to get the result you want.
If you only want to remove the HTML tags, I think you might want to take a look at strip_tags. But I guess that's not really what you're after.
Finally, there's a really really cool new kid on the block - a PHP extension that uses the WebKit HTML and rendering engine to generate PDFs. It is called the libwkhtmltox extension and can be found here. It produces amazing results. For example, look at this PDF it made from the homepage of the New York Times - see (http://www.2shared.com/document/kYuS_G7p/nytimes.html - click "save to my pc" down below). That output was generated without specifying any additional options. Exceptional. HOWEVER, you need to get it running as PHP extension, and that might be a non-trivial task. So I would say: stick with the TCPDF library for now.
您无法使用 PDFLib 从 HTML 生成 PDF 内容。好吧,你可以编写转换器,但这是一项艰巨的工作。
我建议使用支持 HTML 输入的 TCPDF。
You cannot generate PDF content from HTML by using PDFLib. Ok, you can write convertor, but that's a huge work to do.
I recommend using TCPDF which supports HTML input.
伙计...我曾经试图做同样的事情。直接使用 pdflib 是非常困难的。
我找到了这个项目,托管在 Google 代码网站上。该项目值得 10 星(满分 5 星)。
它称为 DOM PDF,您可以在此处下载。您可以通过 CSS 支持从 HTML 页面生成 PDF。只需不到 10 行代码即可生成 PDF!
查看示例以了解其实际效果。
Man... I was trying to do same thing once. It is very hard to work with pdflib directly.
I found this project, hosted on Google code site. The project deserved 10 stars out of 5.
It is called DOM PDF, you can download it here. You can generate PDFs from HTML pages, with CSS support. You can generate PDF with less than 10 lines of code!
Look at samples to see it in action.