如何使用 php 将我的 php 页面导出为 PDF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17795124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I export my php page to PDF using php?
提问by Lalhriatpuii Mapuii
My php page has a lot of variables and style sheet and html tables are also there. I know that there are some opensource pdf class like fpdf, but when it comes to php variables and when I need to pull data from mysql table, I have no idea how to put them together. I have no knowledge in pdf class also. I have also search in internet and could not find what could be the best option to start with for a newbie like me. So any suggestion is welcome.
我的 php 页面有很多变量,样式表和 html 表也在那里。我知道有一些像 fpdf 这样的开源 pdf 类,但是当涉及到 php 变量以及当我需要从 mysql 表中提取数据时,我不知道如何将它们放在一起。我对pdf课也一无所知。我也在互联网上搜索过,但找不到像我这样的新手的最佳选择。所以任何建议都是受欢迎的。
采纳答案by Mawia HL
You can try mpdfclass.
您可以尝试mpdf类。
Just download mpdf class and use the following script to print your php page. Save the below code as mypdfgenerator.php.:
只需下载 mpdf 类并使用以下脚本打印您的 php 页面。将以下代码另存为 mypdfgenerator.php.:
<?php
include("mpdf.php");
$mpdf=new mPDF('win-1252','A4','','',15,10,16,10,10,10);//A4 page in portrait for landscape add -L.
$mpdf->SetHeader('|Your Header here|');
$mpdf->setFooter('{PAGENO}');// Giving page number to your footer.
$mpdf->useOnlyCoreFonts = true; // false is default
$mpdf->SetDisplayMode('fullpage');
// Buffer the following html with PHP so we can store it to a variable later
ob_start();
?>
<?php include "phppage.php";
//This is your php page ?>
<?php
$html = ob_get_contents();
ob_end_clean();
// send the captured HTML from the output buffer to the mPDF class for processing
$mpdf->WriteHTML($html);
//$mpdf->SetProtection(array(), 'user', 'password'); uncomment to protect your pdf page with password.
$mpdf->Output();
exit;
?>