Reputation: 837
I've 'Download In pdf' button on my page. The page contains php and html code.I want to download page as pdf(not open in browser window). I tried one code using jspdf which coverts div contents into pdf format but not applying css and same formatting as per original page.
js code:
<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>
page div:
<div class="container" id="div-bill">
//page elements here
</div>
Upvotes: 1
Views: 2827
Reputation: 1247
Take a look at phantomjs, it does a great job at rendering web pages as PDF documents. It is basically a webkit based browser and supports CSS, Canvas and SVG.
The executable will run on Windows, Linux or OSX servers and you can script actions in 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. Then, you read that pdf file again using php and send it back to the user as a download prompt, for example:
header('Content-disposition: attachment; filename=somename.pdf');
header('Content-type: application/pdf');
readfile($file);
Upvotes: 3
Reputation: 492
You should look into wkhtmltopdf which can render websites into pdf. Wkhtmltopdf will also wait for elements to be generated by JavaScript.
Upvotes: 0