I have a web page which has mathematical symbols in it which is rendered using JavaScript. I have used ASCII Math plugin and ASCIISVG plugin to create and display mathematical expressions in web page. Now I have a requirement to convert this web page to PDF. I have tried dompdf and fpdf, but both failed since these convertors do not support Javascript.
Is there any way to achieve this?
If you rely on JavaScript you have two options:
Render on the client and capture the generated document content. You can then send that content to the server for rendering to PDF. dompdf does not currently support SVG, so with this method you would have to use another output format or another library (I'm not sure which ones support SVG). The main drawback is that this does require user interaction.
Use a headless browser. These are full browser engines compiled into a command line client. These usually support the full web stack (HTML, CSS, JavaScript, SVG). The web content is rendered in memory after which you can export to PDF and image formats. The best I've seen so far is PhantomJS. The only drawback is that you have to be able to install binaries on your system and execute them.
Related
I just want to ask if there are any free/opensource or are there even any existing PHP class library that can convert HTML to PHP using the tags as its image source?
Because I have created a web app that contains Google Maps in it and now I want to save the generated map, instead of saving it using printscrn I would like to convert it to PDF. Is it even possible?
Can you give me guys a link to the library if ever there are existing ones?
I have already looked for fpdf and tcpdf but on their examples all images is contained on the file directory.
Thanks
There is the library HTML2PDF (on github) licensed under LGPL. It is based on TCPDF and very easy to use.
I have tried and successfully converted html to pdf using wkhtmltopdf[1] binary sometime back (including images).
Download wkhtmltopdf binary which is compatible to your base server o/s and use a php wrapper class similar to this[2] (i haven't tested this wrapper class[2] but it should serve your purpose)
There are plenty of php wrappers available for wkhtmltopdf binary, so you can find one easily if this[2] didn't work well for your purpose.
[1] http://wkhtmltopdf.org/
[2] http://mikehaertl.github.io/phpwkhtmltopdf/
I have to generate few reports in PDF format with some inventory stats (no graphs, only tables). Additionally, I have to generate some pdf labels for the placed orders and units in a nice tabular format (taking care of landscape orientation and line wrapping) for the web platform. Which PHP API/Library would be best suitable for this purpose. I am using Zend framework but Zend's PDF API is not rich enough to serve the cause.
One option I am considering is to use LateX for generating PDFs.
Advices? Suggestions?
There are several PDF generation libraries and executables.
I've used:
TCPDF
DOMPDF
html2pdf as #redreggae suggested
wkhtmltopdf
Many other alive & dead solutions
They all rendered HTML to PDF. The problem of all (except wkhtmltopdf) was that they all used different (non-standard) rendering engines and results were often different between them and unsatisfying. wkhtmltopdf uses WebKit to interpret the HTML and create a pdf file. I personally prefer wkhtmltopdf after trying/using (in production) all other of the ones listed.. There is one drawback to it - it is an executable and as such it must be called with exec() however this should not be a big issue when proper coding is applied such that you prevent code injection.
If you want something higher level than HTML to PDF converters, you can try PHPJasperXML, it's a renderer for JasperReports on pure PHP.
What is, according to you, the best way to convert uploaded files of any kind (.doc, .docx,...) into a pdf-file using nothing but php. Is it even possible to do so?
I looked at FPDF, but this creates the pdf files from text.
An other solution previously given was to use the PDFlib library on your server, but unfortunately, my server doesn't support this library...
What is the best way to convert to files my users upload on my site to pdf files?
A simpler approach would be to restrict uploads to .PDF format programmatically and require your users to only upload .pdf files. Provide a link on the upload page to a free and open source pdf printer (e.g. Cuteftp) that the user can install to create .pdf documents from any file that can be printed.
Trying to do it through PHP will be problematic because the uploads could be generated from many different programs that would be impossible to cater for in their entirety. e.g. How would it handle Scribus or ABC Flowcharter or any other 'non-standard' application someone used to create a document?
Much better to filter the upload upfront.
The best server-side PDF generator from those I tried was, so far, wkhtmltopdf, a WebKit-based, self-contained invisible browser that can render any HTML+CSS and generate a PDF from it. Reasonably fast and fairly reliable, has some useful PDF options, such as page size, orientation, etc.
The second part of the job in your case is to convert documents to HTML prior to feeding them to wkhtmltopdf. If possible, have your users upload the docs in HTML (Word and Co. can export (crappy) HTML). If this is not an option, you will have to find a tool just for that, which, in my opinion, is much easier than finding a tool that converts Word docs directly into PDF.
Good thing about wkhtmltopdf is also that you can feed the output of your PHP script to it using the ob_xxx() functions.
PHP Excel best simple way to create doc, docx, xls, xlsx, pdf files with PHP. Its lot easier with clear documentation.
Use Microsoft Office to render Microsoft Office documents, if you care about accuracy at all. This is easily done by invoking Office over COM.
Get access to your server, and install what you need. Doing so would be far easier than monkeying around with sub-par solutions.
Well... I can think of one way of doing it quite easily, but it doesn't involve using PHP.
Upload your documents to a folder on your server, that are browsable by your users.
EG: http://mysite.com/docs/
Then get your users to install a virtual printer driver such as Primo PDF
http://www.primopdf.com/index.aspx
then they can load the document into their browser, and print to PDF for offline browsing.
If this is not an option, and your dealing with office documents that conform to the openXML standard, you could attempt to parse the XML doc into a PHP page for display in the browser, then use JavaScript to trigger a print.
Unfortunately, it does still depend on your user having a PDF printer installed.
Alternatively, you could just load the docs natively, and print to your own PDF printer, then upload the PDF's to the web server for download.
I can't think of any easy way of doing this otherwise, without installing all sorts of different document parser tool-kits and doing a huge amount of behind the scenes work.
I was wondering if there was any way of turning an entire HTML page into a png (or other kind of image?) I'm trying to create PDFs on the fly, but it's pulling across my styles as text, but I want the styles to stay the same as the page (cufon and all). Any help would be appreciated! :)
This doesn't look straightforward. The backend (PHP etc.) doesn't do rendering, layout. It merely generates content.
The layout and visual aspects of the website are done by your client (browser) and the backend has no way of accessing this.
However, given an HTML file, there are libraries that can render it into a PDF like Prince XML that seem to be capable of this.
The only way to generate an image identical, or even near, what a visitor sees in their browser when viewing your site is to launch a browser and take a screenshot. You need the browser's rendering engine to render the page. All the libraries you find to do it without a browser create something much different than what the visitor sees, and won't render cufon or other fancy things at all.
Companies that offer screenshot previews of a webpage now run many servers, each running many virtual PCs, each running a full operating system and real web browser. They have all those systems pulling jobs, opening the webpages in real browsers, taking screenshots and saving images. You won't replicate that with a little PHP script.
http://ipinfo.info/html/rendering_services.php
Turning web pages into images and PDFs is a royal pain using PHP. Solutions often require OS level scripting, fake printer drivers, or screen capturing, which can make for a rather fragile setup. I ran into the same issue a few years ago and started working on native PHP extension that leveraged the Gecko engine to render HTML to PDF, but never finished it.
The best answer I've seen doesn't quite turn a full web page into a PDF, but instead does XML to PDF. XEP by RenderX is the commercial tool Apple uses to produce developer documentation in many formats, including HTML and beautifully rendered PDFs, from an XML source. The great thing about using the XEP tool in conjunction with PHP is that PHP deals with XML very well, so you can pass generated XML to the XEP binary, let it do the conversion to PDF, then deal with the resulting PDF file in PHP.
consider building a regular PDF file that resembles your web page:
PHP::PDF - constructing using php.
PDF Reference - file structure.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert HTML + CSS to PDF with PHP?
I want to convert a web page to a high-resolution PDF suitable for printing. How can I do this?
If you want to do it in code, have a look at HTML2PDF or FPDF, they are php libraries designed to create PDF documents with code.
If you combine HTML2PDF with PHP's output buffering functions
ie
<?php
ob_start();
?>
// put all your html in here
<?php
$data = ob_get_contents();
require('html2pdf.php');
$pdf = new HTML2FPDF();
$pdf->AddPage();
$pdf->WriteHTML($data);
$pdf->Output();
?>
Obviously that is a very rough example but I have used that before within php to generate pdfs. It does have some issues with stylesheets, it seems to ignore most of them. But more control can be gained by using FPDF by itself and manually building the documents.
Try: "wkhtmltopdf" it just work great (and its -open source-). It uses webkit engine (used by safari, chrome browsers) and it is very easy to use :
wkhtmltopdf stackoverflow.com/questions/1878512/ output.pdf
Try it! for Linux and Windows as well. Easy to install. If you use ubuntu, type:
aptitude install wkhtmltopdf
if you want to convert web page using browser, you can use sofware in windows like cutepdf, dopdf etc.
but if you want to convert a web page into pdf by php code (on the fly), there are several ways to convert:
Using library provided, you can try html2pdf (php).
In java / .net a library like pd4ml can be used (even it is not free).
You can use open office converter. It can open multiple doc type, and convert into pdf. Open office provide API (I have tried in java, not php). The flow is same like you open the document, then convert into pdf, but you just do it in your coding. Must have open office server daemon to listen the converter via your code. See here http://oodaemon.sourceforge.net/
If you want quick solution in PHP, maybe you can use html2pdf.
The advantage you use open office converter is the output will be same as you see in browser (I already test it use oodaemon). If using library I am not sure 100% it will be same if you open in browser, because the layout is depend on CSS. pd4ml generate layout in pdf base on html and css (has it owns CSS parser I think)