I am using FPDF / FPDI to build a PDF templating system. One PDF is used as the background and has a number of editable regions defined within it (stored in database). An editable region could be an image, a text, or both, however this is not my question, just a bit of background.
Some of the backgrounds (FPDI source PDF) PDFs or 'templates' contain embedded fonts, I want to be able to achieve two things and was wondering if this is possible.
I want to be able to use any fonts embedded in the source document in the output document.
I need to enumerate these fonts so that they can be included in the UI's font list.
To be clear I want to reuse these fonts, not just have them applied to the imported page, but offer the option to render new text in one of the embedded fonts.
Any pointers would be greatly appreciated.
First on fonts and PDFs.
If I remember correctly, when a font gets included into a PDF, all of the font can be included, or just a subset of the font covering the character faces actually being used can be embedded. Some of the PDF 'compression' tools can strip the unused font glyphs from the pdf file. (Can someone confirm/refute this?)
As for FPDF (it's the library I use), I don't think it can open a PDF in 'edit' mode. It can simply open a PDF and use it as a background - effectively an image of the document. A quick look through the contributed scripts appears to show no scripts that will open a PDF file for real (besides one that can just get the meta-info on the document).
There are other options for PDF libraries. The demonstration of TCPDF I saw at the following presentation, showed that TCPDF may have more in-built capabilities the FPDF. It may be worth a look into that library?
http://suburbanchicagophp.org/archives/75
I am the orginal question asker, but I lost my temp account so cant comment (admin please merge if you can).
Just to clarify.
We have chosen to use FPDF over over TCPDF due to compatibility problems detailed on the FPDI website as FPDI is the core component in this, dropping FPDI was not an option.
We use FPDI to pull pages from the template document, and we then place text and images (as defined in the database using a web UI) over the top of each page, so the usuage is exactly as FPDI allows.
I found another question on this board asking about pulling embedded fonts from pdf files, the asker was slated for copyright infringing activites which is a huge assumption.
How can I extract fonts from a PDF file with Perl?
Our templates will be coming from our designers, we want to resuse (rather than extract) fonts in the template pdf.
So these are our fonts, we just dont want every machine this pdf passes through for editing to have to not only have the font available to embed, but also to select and embed the correct font. It leads to double, even tripple handling of fonts, and possibly some guess work.
One of the replies in the above post is a very helpful direction, it is a Perl module that can enumurate embeded fonts, which is exctly what I need to display embeded fonts in the UI font list.
Now I need to test if the fonts embedded in a source (template) pdf are carried across to the new pdf, I presume they must be, so there must be a resonable way to reuse them.
Ill post back for completelness when I know for sure, still if anyone has any feedback or knowledge on this it would be greatly appreciated.
p.s. it is correct that some editors will create compressed pdf documents and strip some of the unused font, however it is a simple request for us to say dont use an editor that does this, or you will have to re-embed the font.
To further clarify this, Zend_PDF will allow for this exact functionality, enumerating and then reusing fonts embeded in a PDF, but it will not work with PDF later than v1.4, with v1.4 pdf however Zend works very well:
e.g the following is possible in Zend_PDF and then a font can reused by name:
public static function GetFontList($background)
{
$pdf = Zend_Pdf::load($background);
$fonts = array();
// Get all document fonts
$fontList = $pdf->extractFonts();
foreach ($fontList as $font)
{
$fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
$fonts[] = $fontName;
}
return $fonts;
}
Hopefully this will be helpful to someone, however I need a higher pdf versions than 1.4 so I guess I am looking to modify FPDI.
Related
I have a website build on CakePHP framework. I have already integrated Dompdf library for generating PDF from HTML.
And now for viewing the PDF files, I need a pdf reader that will be embedded into the website with limited capabilities so that the files cannot be printed or shared. Ideally, this would be the layout of the reader. The top would show the title of the document and people can highlight, scribble, add note to the document.
Please try out PdfJs
It suits your needs the most and it has good documentation so you could get started quickly
Recently I've been assigned to a project that requires adding a watermark to every page of a PDF file. However, while the PDF is always the same, the watermark for the PDF has to be custom-made to contain the email of any user who downloads it.
After some digging, I found the FPDF and FPDI libraries for PHP. These libraries allow me to easily add custom watermarks to my PDF files, but by design they cause the resulting PDFs to lack Dynamic Content. The original PDF is quite large, so it requires internal links for easy navigation. Because PDFs edited with FPDI lacks dynamic content, those internal links are removed in the watermarked version of the PDF.
After some more searching, I've found an existing workaround for this problem using an extension of the FPDI library. However, that solution requires a vastly outdated version of FPDI that doesn't seem to function properly on my site.
All that said, my question is this:
Barring the above solution, are there any existing PHP libraries that allow me to add custom watermarks to a pdf file without removing the original PDF file's internal links? I'm willing to pay for a license if I have to, but was hoping for a solution priced, at the very least, under $50.
If you are generating your PDF with your own HTML template than you can easily add a Image Div and make this opacity 0.1 it will place your image like a watermark. And there is also a good project on github. You can look at this https://github.com/binarystash/pdf-watermarker
Here's the solution I've came up with:
Editing a pdf with php a cost effective manner isn't viable. If you're working with a single pdf, use an online service to convert it into an HTML file (there are many), then use php to edit in a watermark for that file document. Once that's done, use a service like Prince HTML to convert that HTML file back into a newly watermarked pdf to download.
My current application requires me to convert a document (doc, excel, ppt, image, etc) to PDF. It also requires to add a front page with several information (variable) and add footer to converted document. It may also require to add a text or a logo image as an watermark.
I have been able to convert document to PDF using LibreOffice. I generated a static pdf and I could merge it with converted document using PDFtk
But I need to generate front page dynamically because for each document, some information will change. I thought of using FPDF to generate front page, convert with libreoffice and merge it using PDFtk. But again, it will require adding footer and possibly a watermark. I think watermark can be done by PDFtk. And footer can be generated by FPDF.
So whole process will look like this:
Created Front page with FPDF and save pdf
Convert Doc to PDF with LibreOffice
Add footer to converted PDF and store with FPDF
Generate PDF with watermark using FPDF (possibly)
Combine above three pdf using PDFtk
I guess I am doing too much of processing. Is there a simpler way to achieve this? Are there any alternatives to achieve this with lesser resources / third party apps/lib ?
If you're open to commercially licenced software, PDFLib+PDI will handle all the tasks you require (after conversion to PDF). It runs natively on most server environments with generally very good performance.
The Library I have used to handle PDFs with PHP is TCPDF. It's open source, you can use HTML to style the PDF (a subset of HTML anyway), handle things like pages. I believe you should be able to perform the functions you require all in TCPDF.
To handle manipulations to images, which you may need for watermarking etc, I used GD which provides numerous functions for manipulating images. If you require some more complex image manipulations, you may also want to look at imagemagick.
All of this should simplify your workflow, and allow you to do most of what you require directly in PHP and HTML.
To decrease the number of different things that I need to do, I will simply use the FPDI library.
So what I am going to now is,
Convert the document into PDF format
Create Front Page using FPDI (which extends TCPDF and uses it's function)
Then I will import the converted document using FPDI ' s import feature
I will also add watermark and footer same time
So I will ultimately eliminate the use of PDFtk and reduce the number of libraries used and different activities required to be done separately.
These thing will be done in two activities only :
Convert to PDF with LibreOffice and
Add a front page, import and add footer and watermark with FPDI
I haven't tested it yet, but DomPDF looks like a nice project.
I was using HTML2PDF before, but it's not updated anymore so moving on.
If you're using such libraries, just make HTML templates (moreover, DomPDF supports PHP execution inside html templates) and have fun ;)
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'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.