I'm trying to convert a HTML page which contains Javascript (that formats certain information after the document is loaded) to PDF, using PHP prefarably.
The javascript code will look something like this:
$(document).ready(function() {
formatProfile();
});
This will format the raw data and replace it to the specific DIV.
However, when I do a render and try to convert it to PDF the DIV remains empty, as if the javascript component is not rendered at all.
I've tried using mpdf, wkhtmltopdf, dompdf and many more but none work as intended. Is there any library that will work or is there any workaround (assuming I still prefer to use the javascript to process and output the results to DIV)?
PhantomJS is probably your best bet here. You'll have to install the software on your server, but it gives a nice implementation of webkit in a headless setup so that you can completely render a page, javascript included and then capture the screen output to a PDF.
EDIT:
You might look at this PHP project which wraps the PhantomJS application in a PHP script for easier web screen capture from within PHP.
You can try this http://www.tcpdf.org/ . Site providing some good examples and also you can design page layout as your own styles. I think this will solve your issues.
PDFreactor 6 can convert HTML to PDF and process the JavaScript. It also provides a PHP wrapper.
Related
So I am useing DomPDF to create my document. It draws everything on canvas. I need the resoult data on one page to become one image on this page. Make it unselectable, so it will look like it was scanned rather then created.
Depending on your solution you may want to look at this package spatie/browsershot. It is for Laravel, but you can see it making use of Puppeteer to take html and create either a PDF or an image.
From what I remember DomPDF is using an older rendering engine, where as puppeteer makes use of Google Chromes engine, so can render newer markup. So may even get a better looking result!
I want the option of converting HTML to image and showing the result to the user. I would be creating an $html variable with PHP, and instead of displaying using echo $html, I want to display it as an image so the user can save the file if they needed to.
I was hoping there would something as simple as $image = convertHTML2Image($html); :p if that exists?!
Thanks!!
As #Pekka says, the job of turning HTML code into an image is the job of a full-blown web browser.
If you want to do this sort of thing, you therefore need to have a script that does the following:
Opens the page in a browser.
Captures the rendered page from the browser as a graphic.
Outputs that graphic to your user.
Traditionally, this would have been a tough task, because web browsers are typically driven by the user and not easy to automate in this way.
Fortunately, there is now a solution, in the form of PhantomJS.
PhantomJS is a headless browser, designed for exactly this kind of thing -- automated tasks that require a full-blown rendering engine.
It's basically a full browser, but without the user interface. It renders the page content exactly as another browser would (it's based on Webkit, so results are similar to Chrome), and it can be controlled by a script.
As it says on the PhantomJS homepage, one of its target use-cases is for taking screenshots or thumbnail images of websites.
(another good use for it is automated testing of your site, where it is also a great tool)
Hope that helps.
This is not possible in pure PHP.
What you call "converting" is in fact a huge, non-trivial task: the HTML page has to be rendered. To do this in PHP, you'd have to rewrite an entire web browser.
You'll either have to use an external tool (which usually taps into a browser's rendering engine) or a web service (which does the same).
It is possible to convert html to image. However, first you must convert to PDF. see link
You may have a look at dompdf which is a php framework to convert a html file to a pdf.
use WKHTMLTOPDF. works like a charm. it converts to any page to PDF ..
a jpeg can be obtained by performing later operation.
http://code.google.com/p/wkhtmltopdf/
Is it possible to convert the html contents including styles to image by php. Please guide me.
Well, you need to render it first. For rendering you need something like browser which can handle js, CSS etc. after rendering you take the image. Php is not yet capable of doing such things. But you can achieve it by creating a php extension that uses browser engine and do the task for you. The extension will be like a bridge.
There are many browser engines. Among them you can use webkit. It renders quite fast. I Prefer it.
Another thing to know. This extension will take a lot CPU and memory in compared to normal php script.
I want to generate PDF from a PHP file that includes HTML controls like textbox, and textarea. I attached CSS in the same. I tried FPDF, DOMPDF and TCPDF, but still I don't get exactly what I want. How do I pass HTML controls with PHP variables and CSS to these libraries?
mpdf is another option that you could try.
EDIT :
Found another solution for it, TCPDF is a FLOSS PHP class for generating PDF documents. Looks more dominating library.
"PRINCEXML" is a good library (not completely free now).
Others:
If your meaning is to create a PDF file from PHP, pdflib will help you (as some other suggested).
Else, if you want to convert an HTML page in PDF via PHP, you'll find
a little trouble outta here.. For three years I have been trying to do it as best as I
can.
So, the options I know are:
HTML2PS: same of DOMPDF, but this one convert first in .ps
(Ghostscript), then, in whatever format you need (PDF, JPEG, PNG). For
me it is a little better than dompdf, but I have the same speed problem.. Oh,
it has better compatibility with CSS.
Those two are PHP classes, but if you can install some software on the
server, and access it through passthru() or system(), have a look at
these too:
wkhtmltopdf: based on webkit (safari's wrapper), is really fast and
powerful... It seem like it is the best one (atm) for converting HTML pages to PDF on the fly, taking only two seconds for a three pages XHTML document
with CSS 2. It is a recent project. Anyway, the Google Code page is often
updated.
htmldoc: this one is a tank, it really never stops orcrashes... The project
seems to have died in 2007, but anyway if you don't need CSS compatibility
this can be nice for you.
** Thumbs Up For Strae.
If I understand your needs correctly I don't think any PHP-PDF class would do that.
Mostly you could insert only text and images to a PDF file, so if you would want something that looks like an HTML element you would need to insert it as an image.
Usually just putting HTML doesn't mean all your elements would stay intact in the PDF . (Different world, after all)
http://www.fpdf.org/ is the site having a great HTML-to-PDF class which work well. I am using it, but you have to first study its functionality and then start.
I wonder to know how can we hightlight text and make annotations using JQuery ?
If this feature not available with JQuery , Is there possibility to make it with JavaScript ?
Actually, Acrobat reader has a fairly robust API for manipulating .pdf documents ...
... and that API happens to be in Javascript.
Here are a couple of links:
http://www.adobe.com/devnet/acrobat/javascript.html
http://asserttrue.blogspot.com/2010/07/workaround-for-acrobat-javascripts-lack.html
http://www.evermap.com/javascript.asp
Whether any of this will work in your particular scenario is anybody's guess.
'Hope that helps...
It can't be done. JavaScript runs on HTML pages and manipulates the DOM.
PDF files are transferred as binary and are viewed using a PDF viewer plugin. Javascript is nowhere in the process.
I had similar problem and my solution for now is to use Kineticjs to draw on a PDF ( highlights it's just a transparent rectangle ). Then I'm packing everything (all drawing) into Json string and sending it to the server where I'm using PDFsharp to process and save.