FPDF + FPDI together? - php

I am using FPDF to generate PDF contracts, however people in office often forgets to print ToS file with them and they want me to add ToS into every contract, but when I try to use both FPDF and FPDI it shows me err 500, I've never worked with those two so I am not sure if I can use it that way.
Thanks in advance, Lucas.
EDIT:
<?
require_once('_inc/fpdf.php');
require_once('_inc/fpdi/fpdi.php');
$doc = new FPDF();
$tos = new FPDI();
$doc->whtml("blahblah");
$tos->AddPage();
$tos->setSourceFile('ToS.pdf');
$tplIdx = $pdf->importPage(1);
$tos->useTemplate($tplIdx);
$tos->Output();
$doc->Output();

Related

How to generate an on-page preview of a generated PDF using FPDF

I'm able to generate a PDF with PHP using FPDF, but I'm trying to create an on-screen preview of the generated PDF rather than jumping straight to the online PDF viewer.
<?php
use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;
require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('pdf1.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
{
$tplIdx = $pdf->importPage($pageNo);
// add a page
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 30);
$src = $target_file;
$pdf->Image($src,170,0, 30, 30);
}
$pdf->Output();
?>
The below code is what I've got at the moment, but i'm stuck on trying to generate it on the same page.
$pdf->Output();
The above segment seems to generate the PDF automatically onto a new page (opens online PDF viewer). I've searched through documentation and forums but can't seem to find anything.
Any help would be greatly appreciated. Thanks!
FPDF creates a PDF file. A PDF file needs a viewer or reader application, which renders the content of the PDF document. You cannot just output it e.g. between some HTML fragments on a website.
So your question is not related to FPDF or FPDI at all but you're asking for a PDF viewer/render which you can use on your website.
The most popular solution for such task would be pdf.js.
For other possible canditates you may try "html5 pdf viewer" on google.

Adding pages from an external pdf using TCPDF and FPDI

I am creating a pdf document using tcpdf which is going well. The issue I am having is that I want to include an external pdf in the middle of the document and then continue to add my own pages afterwards.
I have read that FPDI is the best way to achieve this but I am stuck with trying to implement a solution. All of the examples I have found seem to revolve around using an external pdf as a background or template to the entire document, not just as an insert into a document.
Any help would be gratefully received.
AddPage() Method generates a blank page. Each call generates only 1 page. You need to call AddPage() before useTemplate(); After that you can still add new context.
$pdf = new FPDI();
$pdf->AddPage();
$pdf->AddFont('courier');
$pdf->Write(10, 'page 1 created by TCPDF');
$pages = $pdf->setSourceFile('middle.pdf');
for($i=0; $i<$pages; $i++)
{
$pdf->AddPage();
$tplIdx = $pdf->importPage($i+1);
$pdf->useTemplate($tplIdx, 10, 10, 200);
}
$pdf->AddPage();
$pdf->Write(10, 'page 2 created by TCPDF');

Can TCPDF / FPDI accept PDF as string?

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');

TCPDF and FPDI: Is there a separate version of FPDI for TCPDF than for FPDF

I'm trying to build an header generator for pdf worksheets.
My choice of technologies were:
FPDF and FPDI
Now, I'm transistioning to TCPDF and FPDI, because the powers that be have decided we need water marks (transparencies).
According to the FPDI website, switching is as easy as changing all your require/include statements to say tcpdf.php instead of fpdf.php but in my code below:
//... Code to generate appropriate Graph ...
PDFGenHelper::saveIMG($graph,$imgfilepath);
$filename = "template.pdf";
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile($filename);
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->Image($imgfilepath, 168, 13, 35, 65);
$pdf->Write(5, "This is just a simple text"); //This line
$pdf->Output($dircache.$userID.'_'.$excerciseID.'_package.pdf', 'F');
Seems to generate an error that says:
Warning: Division by zero in Appropriate directory stuff here/lib/fpdf/fpdf.php on line 819
I've removed all the references to fpdf in my code but it seems to still be coming up with that.
Also, when I try to print to the PDF using the inherited methods from FPDF:
$pdf->SetFont('Arial','B',25);
$pdf->Cell(40,10,'Hello World!');
This will generate text. In addition the FPDI website says FPDI has a Dependency on FPDF_TPL file which looking into the file tells me it extends FPDF.
So to my question: How do you configure FPDI to work with TCPDF instead of FPDF?
If the error is rised in lib/fpdf/fpdf.php then you still have required fpdf.php before TCPDF.
Also: If it's just the transparency you're missing, just use AlphaPDF from the script section.

Edit PDF files programmatically in PHP

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 :)

Categories