简体   繁体   English

下载pdf格式的PHP,HTML编码页面。

[英]Download PHP, HTML coded page in pdf format.

I've 'Download In pdf' button on my page. 我的页面上有“以pdf下载”按钮。 The page contains php and html code.I want to download page as pdf(not open in browser window). 该页面包含php和html代码。我想以pdf格式下载页面(未在浏览器窗口中打开)。 I tried one code using jspdf which coverts div contents into pdf format but not applying css and same formatting as per original page. 我尝试使用jspdf编写一个代码,该代码将div内容转换为pdf格式,但未应用css,并且格式与原始页面相同。

js code: js代码:

<script type="text/javascript" src="https://parall.ax/parallax/js/jspdf.js"></script>
var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
 }
};

$('#cmd').click(function () {
    doc.fromHTML($('#div-bill').html(), 15, 15, {
             'width': 170,
             'elementHandlers': specialElementHandlers
});
doc.save('sample-file.pdf');
});

Button: <button id="cmd">generate PDF</button> 按钮: <button id="cmd">generate PDF</button>

page div: 页div:
<div class="container" id="div-bill"> //page elements here </div>

Take a look at phantomjs , it does a great job at rendering web pages as PDF documents. 看一看phantomjs ,它在将网页呈现为PDF文档方面做得很好。 It is basically a webkit based browser and supports CSS, Canvas and SVG. 它基本上是一个基于Webkit的浏览器,并支持CSS,Canvas和SVG。

The executable will run on Windows, Linux or OSX servers and you can script actions in javascript. 该可执行文件将在Windows,Linux或OSX服务器上运行,您可以使用javascript编写动作脚本。

So what you can do is have your "Download in PDF" button go to a PHP file that lauches the phantomjs executable, which requests the file you want to turn into a pdf document which saves that on the server's disk. 因此,您可以做的是让您的“下载PDF格式”按钮转到一个启动phantomjs可执行文件的PHP文件,该文件请求将您想要转换为pdf文档的文件保存在服务器磁盘上。 Then, you read that pdf file again using php and send it back to the user as a download prompt, for example: 然后,您再次使用php阅读该pdf文件,并将其作为下载提示发送回用户,例如:

header('Content-disposition: attachment; filename=somename.pdf');
header('Content-type: application/pdf');
readfile($file);

You should look into wkhtmltopdf which can render websites into pdf. 您应该研究wkhtmltopdf ,它可以将网站转换为pdf。 Wkhtmltopdf will also wait for elements to be generated by JavaScript. Wkhtmltopdf也将等待JavaScript生成元素。

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

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