Zend PDF - Is there a nice approach to fulllfill the MVC pattern? - php

Given some user data.
Depending on the controller this data should be rendered either into a PDF or into a HTML file.
I really like the Zend way of the .phtml views combine HTML with very basic PHP.
Is there a similar approach for PDF and/or XLS that allows co-workers without strong programming background to change the PDF / XLS output?

Well, not really for people without a strong programming background.
Basically, rendering a PDF or an Excel view is the same as rendering an HTML View, so you could put the generating code into .phtml files, generate the appropriate output and serve it with the appropriate content-type. It's just a different representation of the data in the Model then. You can utilize ZF's context switch helper for this.
But, HTML is markup and PHP was designed to be embedded in it, so this is cake to do - if you know the appropriate frontend technolgies, like HTML, CSS and JavaScript.
With Excel, you could use the Microsoft's SpreadsheetML and embed PHP into it the same way. This requires the co-worker to have some knowledge of that markup language and XML in general though.
I am not aware of any markup for PDF files, so this has to be generated through an API (likely Zend_PDF) completely. Well, you could probably invent something to define which data goes where in XML, but that would take some thought.
Edit since this is tagged Zend Framework, you might also want to consider using the LiveDocX service, which uses a template based approach and can create PDF and Excel.

Use WKHTMLtoPDF to create a PDF from the HTML rendered view.

HTML and PDF are different planets. Making output libraries that supports both media-types and their respective strengths is almost undoable.
The best solution I can think of is pandoc, http://johnmacfarlane.net/pandoc/
It's not a MVC of it's own, but used as library it may suit your needs.
regards
//t

In an MVC world, the PDF would just be another view. So if you had /controller/action/view.phtml then you should theoretically be able to have a URL like /controller/action/view.pdf.
It's similar to how you can choose your output format with an API such as, say Twitter's. Hitting http://api.twitter.com/version/statuses/public_timeline.json will return the result in JSON; hitting http://api.twitter.com/version/statuses/public_timeline.xml in XML; and so on.

Some of the PDF libraries also include the ability to import/display HTML in the PDF contents, several with advanced properties for handling various formats and unsupported tags. Most that I've used have had strict stipulations about which DTD you need to adhere to.

Related

Export a web-page to a PDF with FPDF

Complicated question with a (hopefully) simple answer.
I'm looking into FPDF to export a web-page to a PDF. The web-page has been formatted for Print media, but since print media can be kind of iffy with CSS/HTML rendered object, I was hoping exporting to a PDF and then printing would make for less of a design headache.
I've scrounged through the FPDF website and I'm not seeing a function for the one thing I was hoping to see. Some kind of include() function. I see a bunch of lines about formatting content, which I'm hoping means
Am I over thinking this? Can I simply use include() after the FPDF constructor and then any content from there in will be printed with the page?
The FPDF library is not meant for directly rendering HTML markup into a PDF.
If that's what you're looking to do, you could try one of the solutions designed specifically for that purpose. For example, the html2pdf library, which among other things is based on the FPDF library. You can also consider using an external API like DocRaptor, which is a cost-effective solution based on the very costly Prince XML program.

Get PDF output from XML generated by a PHP file and translated with an XSLT

I've used a couple of days to think of a best practice to generate a PDF, which end users can customize the layout for themselves. The PDF output needs to be saved on the server or sent back to the PHP file so the PHP file can save it, and the PHP file needs to know that it went OK.
I thought the best way to do this was to use XML, XSLT and Apache Cocoon. But I'm not sure if this is possible or if it's a good idea since I can't find any information of people doing anything similar. It cannot be an uncommon problem.
The idea came when I read about Cocoon converting XML through XSLT to PDF:
http://cocoon.apache.org/2.1/howto/howto-html-pdf-publishing.html
and being able to take in variables:
http://old.nabble.com/how-to-access-post-parameters-from-sitemap-td31478752.html
This is what I had in mind:
A php file gets called by a user, the php file generates a source XML file with a specific name
The php file then makes a request to Cocoon (on the same web server) to apply the user defined XSLT on the XML file. A parameter will be needed here to know which XSLT to apply.
The request is handled by the PHP file and then saved as a PDF on the server, and can later be mailed away.
Will this work at all? Is there a better way to handle this?
The core problem is that the users need to be able to customize the layout on the PDFs themselves, and I need the server to save the PDF and to mail it later on. The users will use it for order confirmations, invoices, etc. And I wouldn't like to hard code the layout for each user.
I've had some good results in the past by setting up JasperReports Server and creating reports using iReport Designer. They're both available in F/OSS ("community") editions, though you can pay for support and value-adds if you need those things.
This was a good solution for us, since we could access it via the Java API for our Java system, and via SOAP for our PHP system. The GUI designer made tweaking reports very easy for non-technical business staff too.
I use webkithtml2pdf to generate my PDF:s. Just create a document with HTML and CSS for printing like you would usually do, the run it through the converter.
It works great for generating things like invoices. You can use SVG for logos and illustrations, and they will look great in print since they are vector based. Even rounded corners with dotted outlines works perfectly.
A minor gotcha is that the input html must have th htm or html file name suffix, so you can't use the default tempfile functions.

PHP generate HTML convert to JPG then to PDF

I'm developing an app where the user adds items to a list. That list is stored in an array and passed to PHP with JSON.
The objective is to then create a PDF with all the values extracted from the user. The PDF is quite complicated. It includes images depending on what the user selects and the text varies depending on the images and the input data.
The first idea was to generate the pdf in php with one of those pdf libraries, but that's going to be a real hassle.
Then I thought of creating an html & css (much easier) and the convert it to PDF. But since the html & css are quite complex I don't think those pdf converters will work with this.
Then I thought I could convert the html to jpg and then to pdf.
It'll be much simpler if I could just use html but the output needs to be pdf.
What do you suggest?
Here's a post that discusses creating PDF files with PHP and the PDFLib extension.
Generate PDFs with PHP it's on sitepoint.
Or if you want to go from HTML to the PDF it looks like TCPDF might work.
You can try using FPDF
Then I thought of creating an html & css (much easier) and the convert it to PDF. But since the html & css are quite complex I don't think those pdf converters will work with this.
wkhtmltopdf to the rescue! If you are on a VPS or dedicated machine, it's probably the best (open source) HTML-to-PDF engine out there. It leverages Webkit, the rendering engine used by Google Chrome and Apple Safari, amongst others.
Otherwise, your only other options are going to involve drawing every aspect of the PDF or image yourself, "by hand" in your code.

TCPDF / HTML2PDF

I am trying to understand what is the interest of HTML2PDF versus using directly TCPDF.
In order words, What are the benifit of HTML2PDF ? Why it's better that use directly TCPDF ?
as you know HTML2PDF (for PHP 5.x) is based on TCPDF.
HTML2PDF handles HTML better than TCPDF, and HTML2PDF can use CSS Classes, and have a couple more of cool functions, specially for dynamic content which TCPDF alone does not handle well. think of it as "TCPDF++"
Download HTML2PDF or go to their website and look at the examples, they will show you better than me!!
Don't forget to set set_time_limit() when generating large documents, to know why look # this post.
For some developers, it is faster to write a template that produces specific HTML instead of expressly making all of the necessary API calls to build the PDF one piece of text and one box at a time.
I'm personally a big fan of wkhtmltopdf, which uses the popular, powerful and standards-compliant WebKit rendering engine. Most common HTML-to-PDF programs either don't understand CSS, or understand CSS poorly.

How do I replace outbound link URLs in a PDF document, using PHP

I have a PDF document with some external links.
I'd like to parse the document, replace the destination of the links then close (and serve) the PDF document, all using PHP
I know I can do this with PDFLib but I don't want to incur this cost.
I could re-write the document with FPDF or DomPDF, but some of these PDFs are quite complex so this would be a major time investment.
Surely there must be a way to do this directly to PDF docs, using native PHP?
TIA
I don't think there is a text/hyperlink changer class for PHP. The closest products, like pdftk, only does higher-level stuff like merging, splitting and applying watermarks.
Changing a pdf is much more difficult than generating it, so you need to use a pdf editor like Nitro PDF (untested), or why not Acrobat/Illustrator/InDesign.
If you must use PHP, regenerating the PDF:s with one of the free classes seems to be your best choice. I like FPDF very much, it gets my recommendation. If you decide to use it, check out FPDI as well, it can use existing PDF files as a template, maybe it will help you. Good luck!

Categories