Background info:
I got a project to produce a customised PDF on the fly from a given PDF file using PHP. All I need it to do is to replace strings, e.g. search in "template.pdf" for "{Address}", replace with "Street Name".
I've seen links to fpdf/pdfi/dompdf etc., but can't find any useful example code that I could use :s.
Any help / pointers would be greatly appreciated.
fpdf is fantastic, you need to use somthing else to import an exisitng PDF though, See below.
http://www.setasign.de/products/pdf-php-solutions/fpdi/
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pagecount = $pdf->setSourceFile('TestDoc.pdf');
$tplidx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplidx, 10, 10, 90);
$pdf->Output('newpdf.pdf', 'D');
decided to generate html web page (PHP) then use wkhtmltopdf
(http://code.google.com/p/wkhtmltopdf)
to produce the pdf bit of a work around but less hastle
PDFlib (with the additional PDI) from pdflib.com should be able to do this for you. Admittedly it is pretty pricey, so there may be other options, too :)
Related
Hi and thanks by advance for you helps.
I'm trying to write on a PDF with FPDF on PHP.
I'm actually working on WordPress.
If I'm using this code on my first website, it's working well:
if (isset($_GET["obtenir-mon-analyse"])){
$pdfFile = getcwd() . '/wp-content/themes/childtheme/ressources/PDF_analyse_template.pdf';
require_once('library/fpdf/fpdf.php');
require_once('library/fdpi/src/autoload.php');
// initiate FPDI
$pdf = new setasign\Fpdi\Fpdi();
// add a page
$pdf->AddPage("L");
// set the source file
$pdf->setSourceFile($pdfFile);
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 0,0 );
$pdf->Output('I');
}
But, with the same code on another website, the PDF generated by the output function is empty (0kb).
Also, the template is working because FDPF is well detecting the available page number.
PS:
The 2 website are hosting on the same host.
I have not any error.
I really don't know where is the problem.
Thanks a lot.
I think nobody will see this answer but:
On wordpress
With the plugin WP-optimize
If you use the mimify option on HTML, you will not be able to use FPDF
I want to Generate a Pdf in Php Using FDPF. can we draw a table in fpdf. I want to Design a Table In the Image given Using FPDF.Is it Possible. I am new to fpdf Please help me. I want Above table design. Thanks in Advance
you should download an addon called PDF_MC_TABLE or copy it from here: http://www.fpdf.de/downloads/addons/3/
$pdf = new PDF_MC_Table();
$pdf->Row(array("this\nis a test","with a multi\ncell"));
$pdf->Output();
You can try this.. It is not the correct design for what you are looking for, but it will helpful to you to create a table.
require("fpdf.php");
$pdf->SetFont('Arial','B',16);
$pdf->SetTextColor(192,192,192);
$pdf->Cell(60,10,'Name:',1,0,'C',false,0);
$pdf->Cell(130,10,'Ali',1,1,'C');
$pdf->Cell(60,10,'Subject:',1,0,'C',0);
$pdf->Cell(130,10,'Maths',1,1,'C');
$pdf->Cell(60,10,'Maximum Marks:',1,0,'C',0);
$pdf->Cell(130,10,100,1,1,'C');
$pdf->Cell(60,10,'Marks Obtained:',1,0,'C',0);
$pdf->Cell(130,10,88,1,1,'C');
$pdf->Cell(60,10,'Percentage:',1,0,'C',0);
$pdf->Cell(130,10,88,1,1,'C');
$pdf->Output();
Is it possible to feed TCPDF or FPDI PDFs as a string? I have an incoming array of PDFs as strings, and I can't write to disk. I wasn't able to find anything in the documentation about this.
If not, is there an efficient way to store/read these PDFs from memory or as objects? as to feed them to FPDI?
If you look at the setSourceFile method documentation, you will see that you can also pass a resource or a StreamReader. What is very interesting with the StreamReader is that it also shares a createByString method. So you can use it like this:
use setasign\Fpdi\PdfParser\StreamReader;
//...
$myData = ... ;
$stream = StreamReader::createByString($myData);
$pdf->setSourceFile($stream);
//...
This will avoid any code duplication... hope this helps someone in the future...
FPDI does not accept strings, but TCPDI, which I just released, has a setSourceData() method in addition to FDPI's setSourceFile(), as I happened to have the exact same requirement. TCPDI has its own parser (tcpdi_parser, based on TCPDF's parser) which supports PDFs above 1.4 without requiring the commercial addon for FPDI - which may also be of benefit when working with existing PDFs.
you can used stream wraper ,..
you can write wraper.php from class link above
header('Content-Type: text/html; charset=utf-8');
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');
require_once('wraper.php');
// Creating new page with PDF as a background
$pdf = new FPDI();
$varInAnyScope = file_get_contents('proposal0.pdf');
$pdf->setSourceFile(VarStream::createReference($varInAnyScope));
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
$pdf->Write(0, "Stack overflow");
ob_clean();
$pdf->Output('WorksheetTest.pdf', 'I');
I have a doubt about using TCPDF and FPDI together.
I am working on a project where I need to modify existing PDF file and generate new PDF, actually existing PDF are Greeting card template and I have to print certain data at certain pages (such as Image on first page, message on 3rd page, artwork on 4th page) to generate final PDF.
I googled and found with TCPDF, it is not possible to manipulate existing PDF, they suggested using FPDI for opening and manipulating existing PDF.
That's where i am stuck. I need TCPDF (it methods to print images, transparent images, utf text, embed font etc) to do what I want to do, but, I need FPDI to start with. :(.
Please help me: is it possible to use both FPDI and TCPDF together? so that I can use features offered by both APIs ?
Thanks in advance...
http://www.setasign.de/products/pdf-php-solutions/fpdi/about/
"As of version 1.2.1 FPDI can be used with TCPDF - a derivate of FPDF."
libraries_load('tcpdf');
libraries_load('fpdi');
$pdf = new FPDI();
$pdf->setSourceFile("%local_file_path%");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage('L', array(3.5, 2), FALSE);
$pdf->useTemplate($tplIdx, 0, 0, 3.5, 2, false);
I was using Drupal at the time so I used libraries_load but require_once should work.
I have several PDF templates that I would like to load and modify and output using tcpdf.
Is it possible to load an existing PDF and use it as a starting point in tcpdf?
You want to use FPDI.
There's some example code here.
I have tried the free version of FPDI but does not support PDF version 1.5 or higher.
If someone else is looking for a free solution I have used TCPDI. You can find it on github https://github.com/pauln/tcpdi
If you are using composer, you can find some fork for composer too. Just search tcpdi on github.
Once you add it to your project, the code is quite simple. It is an extension of TCPDF so all your previous code keep working
This is a snippet from my code. I used it to save a copy of the privacy policy (a static pdf) with the user name and agreement date on each page footer.
// Create new PDF document
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
...
// Add the pages from the source file.
$pagecount = $pdf->setSourceFile($policyPdfPath);
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($tplidx);
// Add agreement text in document footer
$pdf->SetXY(15,282);
$pdf->Cell(180, 5, "Documento approvato da {$fullName} il {$date}", 0, 0, 'C');
}
// Send PDF on output
$pdf->Output(FOLDER_PATH . DIRECTORY_SEPARATOR . "{$userId}.pdf", 'F');
For anyone else finding this, it does appear a PARSER and import class were built for TCPDF (https://tcpdf.org/docs/srcdoc/TCPDF/source-class-TCPDF_IMPORT/#50-100) but as of 2018 was still under development.
Its also worth noting that the solutions above do not allow the contents of the PDF pages to be edited. In other words you import the page as a whole, you cannot edit text content or images.
You can use fpdf with fpdi. You can’t modify directly a template, but you can add a text cell with a white background to cache the old content (note that the old content can be read by using some tools). Then save your new pdf. This tools are relatively easy to use. To resolve the fact that fpdi not read 1.5 pdf version in the free version, you can convert 1.5 version in 1.4 version by using ghost script with the exec command. I use this and work fine.