Dynamic PDF generation - php

this is what i'm trying to do. I have a Student Result Application in
which i'll like to print out a pdf format of a specially designed
Result's Sheet..
http://www.4shared.com/photo/yg8vCjYe/results_layout.html
My question is that is it possible to send all the html, css and php
variables from the final result sheet to the pdf engine, or just
design a new page result_printout.php page and implement the
pdf engine on that page.
I'll be happy your honest opinions
thanks for you help

honestly, i haven't done this before,
but i think this should help:
http://www.rustyparts.com/pdf.php

Since you allready have the HTML I would suggest to use wkhtmltopdf. There also some wrappers for PHP. It's allways a bit tricky to get it all work in the right way, especially with pagebreaks.
But I find it usually more easy to use then all the other PHP PDF creation classes/libraries.

I have only worked with MPDF and you can pretty much send all the html, css and php variables to the pdf enginge and tell the engine to either force a download or send it to the browser where the user can either save the file or print or just view it. You can also email the generated pdf.
You can read the documentation here: http://mpdf1.com/manual/

Related

Render Html + CSS page as PDF without using API and libraries but using header type

I want to display Html page generated using PHP and CSS directly as a pdf.
I have searched a lot for this but endup with lot of libraries to do these task. But I don't want to use any libraries and just want to render it as PDF by defining header tag.
I am not sure but, have little in memory it is possible without using any API, library which might be wrong so want to confirm before proceeding to another solution.
Hope there might be the simple way to do this, as this is the most required feature for most of product.
First of all, I'd recommend using a library for this because although it "might be wrong", it's probably more correct than what you or I would come up with. Second, (most of) this isn't done using headers ;)
If you REALLY want to do it yourself, the native PDF functions in PHP are found here, which libraries like FPDF (my personal favorite) use directly in their implementations.
To print a page to PDF, you'll have to render it first (create the HTML DOM and apply the CSS) which PHP does not do. You'll have to render the page first with an HTML engine like WebKit, and then return to PHP for the print operation.
Again, this should probably be done using a library, saving you headaches and debugging. Of course, this question has been asked before so there's plenty of info available.
But if you want to have a go yourself, that's the path you must walk (or crawl). Me, I'd take the bus.
UPDATE
To answer your question in the comments:
<?php header("Content-type: application/pdf;charset=UTF-8"); ?>
Putting this on the top of the page render the pdf but it's not
working generated in this way. So, can you suggest any thing for this.
That header is for an HTTP request. When the request is returned from the server, it sends a header suggesting what type of content is being returned (JSON, an image, a pdf, whatever).
In other words, that header doesn't actually make a PDF, it just tells the browser to expect a PDF. After the header, the server then sends something (hopefully, a PDF).
That's why I said your original question isn't done using headers.

Create a PDF on server-side with some data and then create a download link for the user

i want using php generate a pdf in my site with some format and some values i have into some vars, any suggestion of a good script and good practices to know about? Thanks!
You can make an ajax call to your php and then push the generated PDF to the browser.
you can create PDF server side with this.
Using a HTML to PDF generator is probably the best way. The one I have found the easiest to use, and use regularly, is http://www.tufat.com/s_html2ps_html2pdf.htm
It can be daunting at first, but once you get used to it it's fine.
You might want to checkout TCPDF. Have a look at the examples, and see how easy it is to create PDFs with PHP.
if you have shell access, you can use http://www.pd4ml.com/ for converting html to pdf. its really easy, but its not free and you need shell access.

PHP to Mesh 2 PDF'S Together or not?

I have an already pre-designed PDF, and I would like to fill the PDF with some database information. So I'm curious if I should save the PDF into JPG's and render them out with the data on top of the image and re-create a PDF.
Or is there a way to use the PDF already, and print data into the PDF that is already made?
I am trying to figure out the best solution to generating this type of PDF.
All thoughts would be greatly appreciated!
You can read about PHP's PDF library here: http://php.net/manual/book.pdf.php
PHP also recommends these alternatives:
http://www.fpdf.org/
http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
http://php.net/manual/book.haru.php
PDF files support the use of interactive form fields, so the best method for adding information to a PDF from a database, is add form fields to your database and find a library that let's you fill out PDF forms programatically.
Rendering a PDF to an image just to write information on it and then converting it back to a PDF would mean that no text was selectable in the new PDF unless you OCR'd it, which isn't the most optimal way to do it.
After a bit more digging, I was able to use Zend PDF within the framework to update the PDF quite easily. It was my first time using the Zend Framework, and found it quite useful. I suggest anyone trying to manipulate PDF's use Zend.
Just my .02 though. I appreciate your pointers though that were given.

Creating PDF's using PHP

What I am wanting to do is create a PDF ideally from HTML code. I found a class called dompdf but I'm having issues with font and page breaks.
Does anyone know of another script or even a better way in general to generate PDF files?
The reason why I am converting HTML to PDF is because I want someone to use a WYSIWYG editor to create the contents and click save to generate their PDF file...
Any input would be greatly apprecaited
Due to the different nature of PDF and HTML you'll always have to make a few comprimises when trying to convert HTML into PDF.
If this doesn't bother you too much, I'd get started with TCPDF. Its easy to use and has a fairly good grasp of colors, sizes and some other style related HTML attributes.
i use this class TCPDF :
http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
If you don't mind a bit of work then you can use Cairo to generate PDFs.
You can take a look at html2fpdf. It can accept very basic html and css.

Direct Print webpage in PDF file

In my site i m fetching my mysql data by using PHP. I want open that data in pdf file when i click pdf print button is it possible?
First of all, if you want a high quality professional product to do that. You want Prince XML
If you are looking into some open source tool to achieve something similar. You can look into this SO question.
You could prepare static PDF form file, that just fill it in with values using PHP's FDF module.
It depends which platform are you using. This would be an easy job if you are using Groovy on grails. There are plugins which facilitate pdf reporting like the jasper-plugin.
Luis
Check out jsPDF, an open-source library for generating PDF documents using nothing but JavaScript.
You can process the data with Apache FOP after transforming it to XML. (http://xmlgraphics.apache.org/fop/).
If your page is template based, you may create a template which produces xml output and process that. You'll have extremely well contol over the pdf construction. The tradeoff is that it is not a "plug this in and will work" solution, but I've done that and once its set up, works like charm.
I've used TCPDF in the past, it's a little kludgy but can definitely get the job done. (http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf)
The FPDF module in PHP is simple enough to get the data together. It is a safe option since you know what data you are passing out to the PDF engine. There are some streaming pdf options which can take in a bunch of html and then output that to pdf however they can get it quite wrong without you knowing.
I used, on Linux machines, WKHTMLTOIMAGE/WKHTMLTOPDF a number of times, on many projects. It workes like a charm, easy to use, just a script that you run.

Categories