Add 1 Page to each PDF in folder - php

I have a folder with 100s of PDF versions of PPT presentations. I also have a one page PDF file that I want to add to the beginning of each PDF file. Is there a way I can do this with PHP? Could I maybe use the Zend Framework?

It certainly can be done by Zend_Framework!
$pdf = Zend_Pdf::load($fileName);
$frontPdf = Zend_Pdf::load('/path/to/template.pdf');
$frontPage = $frontPdf->pages[0];
//prepend our template front page to PDF
array_unshift($pdf->pages, $frontPage);
//update original document
$pdf->save($fileName, true);
I haven't tested the code here but we have an application working on the same principle.
Check the documentation for pages within Zend_Pdf if you have any problems.

Related

Merge pdf files in PHP and keep weblinks inside

We have a project where we merge different pdfs to create a catalog.
Right now it's running on myokyawhtun/pdfmerger, which runs fine, but it does not keep links set in acrobat.
We have tried different libraries we found (pure PHP, we cannot install or call applications from the command line via shell-exec or similar on this webspace, so no gs), even if we just import the pdf-files via fpdi and resave them, the hyperlinks get lost.
Is there any (pure PHP) library out there which can retain links inside the files? Or are there some special settings that we missed?
We have tried:
setasign/fpdi
iio/libmergepdf
jurosh/pdf-merge
Example code for the current lib (myokyawhtun/pdfmerger):
require('vendor/myokyawhtun/pdfmerger/tcpdf/tcpdf.php');
require('vendor/myokyawhtun/pdfmerger/tcpdf/tcpdi.php');
require('vendor/myokyawhtun/pdfmerger/PDFMerger.php');
$pdf = new \PDFMerger\PDFMerger;
foreach($sourcePdfs as $file)
{
$pdf->addPDF($pdfDir.'/source/'.$file);
}
$pdf->merge('download', 'Download.pdf');
All the mentioned libraries use FPDI under the hood, which simply does not support content outside of a pages content stream, such as links or any other annotation type.
We (author of FPDI) also offer non-free products which work on another level and which allow you keep all annotations including links and also forms when you concatenate the documents. This is possible with the SetaPDF-Merger component:
$merger = new SetaPDF_Merger();
foreach($sourcePdfs as $file) {
$merger->addFile($pdfDir . '/source/' . $file);
}
$merger->merge();
$document = $merger->getDocument();
$document->setWriter(new SetaPDF_Core_Writer_Http('Download.pdf'));
$document->save()->finish();

How to insert an image from PHP into PDF 1.7

I'm creating a web app that allows a canvas form to insert an image from a HTML canvas into a particular position in multiple PDF files. I had this working with python flask as a back-end but the people that I'm making it for only want it in PHP. I have tried using libraries like FPDI but they only work with PDF versions up to 1.4 while the PDF files we are using are version 1.7.
Does anyone know any possible libraries that can help me solve this issue. I would prefer not to convert the PDF files if possible.
Cheers
With TCPDF you can insert images into a PDF (v.1.7) file:
Requirements
composer require tecnickcom/tcpdf
Example
<?php
require_once __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF();
$pdf->setPDFVersion('1.7');
$pdf->setAutoPageBreak(true);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
// Insert image
$pdf->setJPEGQuality(100);
$pdf->image(__DIR__ . '/example.jpg', 10, 10);
// Close and output PDF document
//$pdf->output('doc.pdf', 'I');
// Save the pdf file
$content = $pdf->output('', 'S');
file_put_contents('example.pdf', $content);
We (Setasign, creator of FPDI) offer a commercial add-on that let you import PDFs which uses a compression technic that was introduced in PDF 1.5.
You may also try to downgrade these documents with an external program. I'm aware of some people using Ghostscript for this.
Generally you should know that you do not insert an image into the existing PDF but you create a completely new PDF while importing a single page into a reusable structure which you place onto a newly created page. On top of this you place the image.
With FPDI you cannot edit a PDF document.

FPDF and PHP: Help merging PDF forms with text fields without losing text data?

I have PDF form files that I fill out dynamically with PHP using FPDM (the FPDF script). I can save them on my server no problem, and the text all looks fine in the PDF when I download and view in Acrobat.
My problem is: I'm trying to merge multiple PDF files together on the server so the user can download a single PDF document with several pages. I downloaded PDF Merger (http://pdfmerger.codeplex.com/) and got it merging the files together, but this causes the PDF form text to disappear.
Anyone know of a form-friendly PHP-based PDF merger that doesn't require installing anything (other than uploading libraries) to my server?
Code that works for merging but kills text in form boxes:
$pdfCombined= new PDFMerger;
$pdfCombined->addPDF('../forms/generated/16.pdf', 'all')
->addPDF('../forms/generated/19.pdf', 'all')
->merge('browser', 'mergedDoc.pdf');
The linked "PDF Merger" simply uses FPDI in the back. FPDI is not able to handle dynamic content as described here.
A pure PHP solution for merging PDF forms is the SetaPDF-Merger component (not free). An evaluation requires the installation of a Loader (Ioncube or Zend Guard). License owners will get access to the source code, so that no external library is needed. The usage is also that easy:
require_once("library/SetaPDF/Autoload.php");
// create a file writer
$writer = new SetaPDF_Core_Writer_Http("mergedDoc.pdf");
// create a new merger instance
$merger = new SetaPDF_Merger();
// add the files
$merger->addFile('../forms/generated/16.pdf');
$merger->addFile('../forms/generated/19.pdf');
// merge all files
$merger->merge();
// get the resulting document and set the writer instance
$document = $merger->getDocument();
$document->setWriter($writer);
// save the file and finish the writer
$document->save()->finish();

Generate PDF from HTML/CSS/PHP in Symfony 1.4

I am working on a Symfony 1.4 project. I need to make a PDF download link for a (yet to be) generated voucher and I have to say, I am a bit confused. I already have the HTML/CSS for the voucher, I created the download button in the right view, but I don't know where to go from there.
Use Mpdf to create the pdf file
http://www.mpdf1.com/
+1 with wkhtmltopdf
I'd even recommand the snappy library.
If you use composer, you can even get the wkhtmltopdf binaries automatically
Having used wkhtmltopdf for a while I've moved off it as 1) it has some serious bugs and 2) ongoing development has slowed down. I moved over to PhantomJS which is proving to be much better in terms of functionality and effectiveness.
Once you've got something like wkhtmltopdf or PhantomJS on your machine you need to generate the HTML page and pass that along to it. I'll give you an example assuming you use PhantomJS.
Initially set what every request parameters you need to for the template.
$this->getRequest->setParamater([some parameter],[some value]);
Then call the function getPresentation() to generate the HTML from a template. This will return the resulting HTML for a specific module and action.
$html = sfContext::getInstance()->getController()->getPresentation([module],[action]);
You'll need to replace the relative CSS paths with a absolute CSS path in the HTML file. For example by running preg_replace.
$html_replaced = preg_replace('/"\/css/','"'.sfConfig('sf_web_dir').'/css',$html);
Now write the HTML page to file and convert to a PDF.
$fp = fopen('export.html','w+');
fwrite($fp,$html_replaced);
fclose($fp)
exec('/path/to/phantomjs/bin/phantomjs /path/to/phantomjs/examples/rasterize.js /path/to/export.html /path/to/export.pdf "A3");
Now send the PDF to the user:
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Content-Description','File Transfer');
$this->getResponse()->setHttpHeader('Cache-Control','public, must-revalidate, max-age=0');
$this->getResponse()->setHttpHeader('Pragma: public',true);
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding','binary');
$this->getResponse()->setHttpHeader('Content-length',filesize('/path/to/export.pdf'));
$this->getResponse()->setContentType('application/pdf');
$this->getResponse()->setHttpHeader('Content-Disposition','attachment; filename=export.pdf');
$this->getResponse()->setContent(readfile('/path/to/export.pdf'));
$this->getResponse()->sendContent();
You do need to set the headers otherwise the browser does odd things. The filename for the generated HTML file and export should be unique to avoid the situation of two people generating PDF vouchers at the same time clashing. You can use something like sha1(time()) to add a randomised hash to a standard name e.g. 'export_'.sha1(time());
Use wkhtmltopdf, if possible. It is by far the best html2pdf converter a php coder can use.
And then do something like this (not tested, but should be pretty close):
public function executeGeneratePdf(sfWebRequest $request)
{
$this->getContext()->getResponse()->clearHttpHeaders();
$html = '*your html content*';
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED, 'whatever_name.pdf');
throw new sfStopException();
}

Open a pdf with PHP and place text in it

I have a PDF which has an image on it (a logo)
I want to open it with PHP and write some text over it and save it back to the file system.
I've done this with the Zend framework before but this project is using code igniter so I need either a standalone lib or a code igniter plugin.
thanks
Zend_Pdf is a standalone lib.
Zend Framework is deliberately designed with a Use-At-Will architecture, so you can use most components with no (or very little) dependencies on other components in the framework.
To use Zend_PDF in Code Igniter, place the Zend/Pdf folder into your CI project's include path, so it is accessible. Then include it with
// Load Zend_Pdf class
require_once('Zend/Pdf.php');
See this (general) tutorial:
http://devzone.zend.com/article/2525
In my esperience, the first question you should ask is: where the original pdf come from?
Do you want to create the pdf from php, as a template, and then insert the text on it in a second time?
Or you create the pdf in other ways, and then fulfill it via php?
In the first case, go with zend pdf, and write down your class to handle it.
In the second case, you may want to take a look to pdftk, that allow you to merge an fdf file with a PDF file.
Here an example with a forms (and the createFDF.php file), but the behavior can be applyed in many other ways...
Its quite simple:
<?php
require_once('createFDF.php');
$data = array(
'field_1' => 'Text 1',
'Field_2' => 'Text 2
);
$FDF_file = 'myfile.fdf';
$PDF_file = 'mypdf.pdf';
$PDF_source = 'your-pdf-original-file.pdf';
$fdf_data = createFDF($PDF_source, $data);
$fh = fopen($FDF_file, 'w');
fwrite($fh, $fdf_data);
fclose($fh);
?>
<h2>File FDF created.</h2>
<?php
passthru("pdftk $PDF_source fill_form $FDF_file output $PDF_file flatten");
?>
<h2>Pdf merged.</h2>
but, you will need to create the original pdf file with forms within by hand (or, as far as i know, there is no tools to create it via php)
You can try to set the following header:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// The PDF source is in original.pdf
readfile(base_url($pdf->URL));
?>

Categories