6

I have a web app and I need print the data into pdf. In back-end I use php, while in front-end a javascript MVC framework (backbone.js).

I don't know if generate the pdf only using javascript (..then using data cache) or using ajax that will call a php page that will generate the pdf... What's the better solution?

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

Thanks.

keepyourweb
  • 664
  • 4
  • 10
  • 19
  • 4
    Javascript: a user's computer will do the job; PHP: your own server does the jobm which could be a problem if there are ~100 users, generating PDFs at the same time. – sascha Nov 08 '11 at 15:16
  • 2
    Relevant? http://stackoverflow.com/questions/742271/generating-pdf-files-with-javascript – Marc B Nov 08 '11 at 15:19

6 Answers6

5

I prefer mPDF to generate PDFs:

You can generate the PDF using PHP and grab it by doing an AJAX call, that's the only good way. Of course in mPDF you can style document using CSS.

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

edit: now it's 2016 and mPDF is for sure not recommended, deprecated piece of software. There exists much better ones, 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

Community
  • 1
  • 1
PiKey
  • 628
  • 5
  • 24
3

Check out DocRaptor if you need the formatting to be precise. They are a SaaS built around Prince XML.

https://docraptor.com/

Here's an example using PHP (outputting to an excel spreadsheet instead of a PDF though)

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);

?>
illbzo1
  • 480
  • 3
  • 13
CambridgeMike
  • 4,562
  • 1
  • 28
  • 37
1

You would probably want to use PHP to build the PDF. Not sure if there are any javascript libraries out there to do such a thing. As @Sn0opy said, this could become a server problem though if many people are generating PDFs (especially if they are large).

There are a number of PDF builders for PHP out there. I've used html2ps and tcpdf both with success.

Paul DelRe
  • 4,003
  • 1
  • 24
  • 26
1

There are several pure js solutions, but they are not very reliable, don't work in all browsers. You can take a 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 limitations and if they are too much for your project use PHP like people advised.

If you know that the client has flash/acrobat plugin installed you can use them on the client to generate PDFs, look here for details: http://www.adobe.com/devnet/acrobat.html

Roman Goyenko
  • 6,965
  • 5
  • 48
  • 81
0

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

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

Also, this article shows exactly what you need to do to generate a PDF in PHP:

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

ckost
  • 1
0

I like a lot this one for php: html2pdf

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71