What is the best way to generate pdf using javascript and / or php?

I have a web application and I need to print the data in pdf format. In the back-end, I use php, and in the interface I use the javascript MVC framework (backbone.js).

I don’t know if the pdf file will be generated only using javascript (then use the data cache) or using ajax which will call the php page that will generate the PDF file ... What is the best solution?

Naturally, I need to be able to print, save and use the stylesheet for pdf.

Thanks.

+6
source share
6 answers

I prefer mPDF to create PDF files:

You can generate the PDF using PHP and capture it by making an AJAX call, which is the only good way. Of course, in mPDF you can style your document with CSS.

If you want to read PDF using javascript, read: Open Source Javascript PDF Viewer

edit: now it's 2016, and mPDF is definitely not recommended, an outdated piece of software. There is much better, just take a look at GitHub:

PHP: https://github.com/search?l=PHP&o=desc&q=pdf&ref=searchresults&s=stars&type=Repositories&utf8=%E2%9C%93

JavaScript: https://github.com/search?utf8=%E2%9C%93&q=node+pdf&type=Repositories&ref=searchresults

+5
source

Check out the DocRaptor if you need formatting to be exact. This is a SaaS built around the prince of XML.

https://docraptor.com/

Here is an example of using PHP (output to Excel spreadsheet instead of PDF)

https://docraptor.com/documentation#php_example

<?php $api_key = "YOUR_API_KEY_HERE"; $url = "https://docraptor.com/docs?user_credentials=$api_key"; $document_content = "<table><tr><td>Cell</td></tr></table>"; $request = new HTTPRequest($url, HTTP_METH_POST); $request->setPostFields(array('doc[document_content]' => $document_content, 'doc[document_type]' => 'xls', 'doc[name]' => 'my_doc.xls', 'doc[test]' => 'true')); $request->send(); $file = fopen ("my_excel_doc.xls", "w"); fwrite($file, $request->getResponseBody()); fclose ($file); ?> 
+3
source

You might want to use PHP to create a PDF file. Not sure if there are any javascript libraries out there to do such a thing. As @ Sn0opy said, this can become a server problem, although if many people create PDF files (especially if they are large).

There are a number of PDF builders for PHP. I have successfully used html2ps and tcpdf.

+1
source

There are several pure js solutions, but they are not very reliable, they do not work in all browsers. You can look here:

http://andreasgal.com/2011/06/15/pdf-js/

here is their cool example: http://mozilla.github.com/pdf.js/web/viewer.html

There is also this project: http://code.google.com/p/jspdf/

Read about the limitations, and if they are too much for your project, use PHP like people do.

If you know that the client has the flash / acrobat plugin installed, you can use them on the client to create PDF files, see the details here: http://www.adobe.com/devnet/acrobat.html

+1
source

Here's the manual PHP entry for the PDF extension in PHP:

http://php.net/manual/en/book.pdf.php

In addition, this article shows what you need to do to create a PDF file in PHP:

http://www.sitepoint.com/generate-pdfs-php/

0
source

I like a lot for php: html2pdf

0
source

Source: https://habr.com/ru/post/901034/


All Articles