I have a pdf template that I made. I am working on a real estate website that asks users for information regarding their properties and displays those answers neatly in a pdf flyer that they can download. My question is what is the best way to go about this?
I downloaded FPDI so I can work off an existing template but I dont see how I can pass PHP variables to the Write() function.
I also downloaded TCPDF but I cant seem to get it to work together with FPDI.
use setasign\Fpdi\Fpdi;
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
require_once('vendor/tecnickcom/tcpdf/tcpdf.php');
//initiate FPDI
$pdf = new Fpdi();
$date = Date('d - m - Y');
$pageCount = $pdf->setSourceFile('C:/Users/19292/Desktop/flyer-template.pdf');
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
if($pageNo === 1) {
//If its the first page of your pdf
$templateId = $pdf->importPage($pageNo);
$pdf->AddPage();
$pdf->useTemplate($templateId, ['adjustPageSize' => true]);
$pdf->SetXY(10, 10);
$pdf->Write(0, 'Title to the left on your first page');
$pdf->write($date,true,0,false,false,'left');
} else if($pageNo === 2) {
$templateId = $pdf->importPage($pageNo);
$pdf->AddPage();
$pdf->useTemplate($templateId, ['adjustPageSize' => true]);
$pdf->SetXY(220, 10);
}
}
$pdf->Output();
?>
not sure what exactly should be required, what should be used to get the output that I want.
Related
I have this script which already sign a PDF
<?php
require("../config/include.php");
require_once(DIR_LIBRERIAS."TCPDF/tcpdf.php");
require_once(DIR_LIBRERIAS.'FPDI/fpdi.php');
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION);
error_reporting(0);
// set certificate file
$certificate = 'file://'.DIR_ROOT.'cert/testcertif.crt';
$pdf = new FPDI();
$filename = "zz_test_firmado.pdf";
$info = array('Name' => 'testcertif', 'Location' => 'Oficina', 'Reason' => 'test firma', 'ContactInfo' => 'test.com.ar');
$pdf->setSignature($certificate, $certificate, 'test key pass', '', 2, $info);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pages_count= $pdf->setSourceFile($filename);
$page = "P";
for($i = 1; $i <= $pages_count; $i++)
{
$tplIdx = $pdf->importPage($i);
$size = $pdf->getTemplateSize($tplIdx);
if ($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
$arrayUltimo = array($size['w'], $size['h']);
$page = "L";
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
$arrayUltimo = array($size['w'], $size['h']);
}
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
}
$pdf->output('testfirmass222.pdf', 'I');
?>
However, when the pdf that i'm importing already has a signature, the signature is replaced by the new one put on the script, is there a way to keep both?
FPDI does not modify an original but you create a completely new one by importing page appearances of existing documents into a reusable structure.
The resulting document is a completely new one which may look identically but its internal structure is a completely different one.
Annotations and for sure digital signatures will not be imported.
Your task cannot be done with FPDI.
PS: In any case update FPDI to its latest version. It looks like you are using a legacy version.
I am working on how to merge pdf_file1 to pdf_file2. I need to merge pdf_file1 to specific pages in file 2. to be able to do that I need to find specific keyword in header of pdf_file2
EX: If (keyword == header_of_pdf_file2) then
Merge file
here is my current code to merge the 2 pdf
<?php
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->setSourceFile("pdf_file1");
$tplIdxA = $pdf->importPage(1, '/MediaBox');
$pagecnt=$pdf->setSourceFile("pdf_file2.pdf");
$x=1;
for($i=0; $i<$pagecnt; $i++){
$tplIdxB = $pdf->importPage(intval($i+1), '/MediaBox');
$pdf->addPage();
// place the imported page of the first document:
if($i==0){
$pdf->useTemplate($tplIdxA, 0, 0, 210,300);
// place the imported page of the snd document:
}
$pdf->useTemplate($tplIdxB, 0, 0, 200,295);
$x=($i+1)*20;
}
$pdf->Output();
I'm looking to start with an initial PDF file, one that has graphics and text, and then take some html code which has dynamic values for some user input, generate that as a PDF, hopefully either using the initial PDF as a background, OR somehow running a PHP script afterwards to "merge" both PDF where one acts as a background to another.
I have some code that renders an HTML formatted PDF: (using DOMPDF)
$initialpdf = file_get_contents('file_html.html');
$initialpdf = str_replace(array(
'%replaceText1%',
'%replaceText2%'
), array (
$replaceText1,
$replaceText2,
), $initialpdf);
$fp = fopen('file_html_new.html','w');
file_put_contents('file_html_new.html', $initialpdf);
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
{
$dompdf = new DOMPDF();
$dompdf->set_paper($paper,$orientation);
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
#file_put_contents($filename . ".pdf", $pdf);
}
$filename = 'HTML_Generated_pdf';
$dompdf = new DOMPDF();
$html = file_get_contents('file_html_new.html');
pdf_create($html,$filename,'Letter','landscape');
The code above takes html file "file_html.html" and does string replacements with user input values, renders this as a new HTML file called "file_html_new.html" and then renders that AS a PDF.
I also have other PHP code that render a PDF by having a PDF as an initial source: (using FPDF)
<?php
ob_clean();
ini_set("session.auto_start", 0);
define('FPDF_FONTPATH','font/');
define('FPDI_FONTPATH','font/');
require('fpdf.php');
require('fpdi.php');
$pdf = new FPDI();
$pdf->setSourceFile("/home/user/public_html/wp-content/myPDF.pdf");
$tplIdx = $pdf->importPage(1);
$specs = $pdf->getTemplateSize($tplIdx);
$pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L', 'Letter');
$pdf->useTemplate($tplIdx, 0, 0);
$pdf->SetFont('helvetica');
$pdf->SetXY(30, 30);
$pdf->Write(0, $replaceText1);
ob_end_clean();
$pdf->Output('New_Generated_PDF.pdf', 'F');
?>
This takes an already existing PDF, "myPDF.pdf", and uses it as a background, writing some passed in value to the document, and saving the newly produced document.
While this is essentially what I want to do, I need to work with html because the exact formatting for text gets rigorous and almost impossible to do just by plotting it in manually.
I'm open to using DOMPDF, FPDF, FPDI, TCPDF, or any other PHP resource in order to accomplish this.
Is there a way to fuse the two ways I have above?
For sure you can use different existing PDF documents with FPDI, too. This code should show you the concept (actually I guess that all page formats are A4 portrait):
<?php
$pdf = new FPDI();
// let's get an id for the background template
$pdf->setSourceFile('myPDF.pdf');
$backId = $pdf->importPage(1);
// iterate over all pages of HTML_Generated_pdf.pdf and import them
$pageCount = $pdf->setSourceFile('HTML_Generated_pdf.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// add a page
$pdf->AddPage();
// add the background
$pdf->useTemplate($backId);
// import the content page
$pageId = $pdf->importPage($pageNo);
// add it
$pdf->useTemplate($pageId);
}
$pdf->Output();
I have a php page that generates a pdf file by using fpdf. How to merge another pdf to the same page by using PDFMerge .
require('fpdf.php');
class PDF extends FPDF {
//some function here
}
some code here..
at the end
include 'PDFMerger.php';
$pdfMerge = new PDFMerger;
$pdf->addPDF('samplepdfs/one.pdf', 'all')
->merge('file', 'samplepdfs/TEST2.pdf');
$pdf->Output('filename.pdf', 'I');
when it executes I want to merge one.pdf and filename.pdf. How to get this? thanks in advance
You should use a native version of FPDI instead the PDFMerger class, because it uses a very old version of FPDI.
<?php
require_once('fpdf.php');
require_once('fpdi.php');
class PDF extends FPDI
{
// ...
}
$pdf = new PDF();
// ...
$pageCount = $pdf->setSoruceFile('samplepdfs/one.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$tplId = $pdf->importPage($pageNo);
$pdf->AddPage();
$pdf->useTemplate($tplId, null, null, 0, 0, true);
}
$pdf->Output('filename.pdf', 'I');
i am trying to merge two files using FPDI the error i get is:'TCPDF ERROR: File is encrypted!', however, the files are not encrypted, at least the files are printable, viewable etc and no password is required.
i want to merge two files:
http://www.nps.org.au/__data/cmi_pdfs/CMI7412.pdf
http://www.nps.org.au/__data/cmi_pdfs/CMI6656.pdf
after i copy the files to the server and store the file names in array ($files) that has the absolute file paths, my code is:
if (count ($files) > 0 )
{
$pdf = new FPDI();
$pdf->setPrintHeader(FALSE);
$pdf->setPrintFooter(FALSE);
foreach ($files as $file)
{
for ($i = 0; $i < count($files); $i++ )
{
$pagecount = $pdf->setSourceFile($files[$i]);
for($j = 0; $j < $pagecount ; $j++)
{
$tplidx = $pdf->importPage(($j +1), '/MediaBox');
$specs = $pdf->getTemplateSize($tplidx);
if ( $specs['h'] > $specs['w'] )
{
$orientation = 'P';
}
else
{
$orientation = 'L';
}
$pdf->addPage($orientation,'A4');
$pdf->useTemplate($tplidx, 0, 0, 0, 0, TRUE);
}
}
$output = $pdf->Output('', 'S');
foreach ( $files as $file )
{
delete_file($file);
}
}
I have also tried to merge the files using ghostscript, but with no luck.
I tried acrobat pro, which required a password for one file, but when I used mac preview, i exported the file and was able to merge it using acrobat with no issues. i.e. mac preview removed the protection with no problems.
So, what is it about the file CMI7412.pdf that stops merging, but not exporting, viewing, printing? and how can i get around it?
I have tried similar issue and works fine, try it. It can handle different orientations between PDFs.
// array to hold list of PDF files to be merged
$files = array("a.pdf", "b.pdf", "c.pdf");
$pageCount = 0;
// initiate FPDI
$pdf = new FPDI();
// iterate through the files
foreach ($files AS $file) {
// get the page count
$pageCount = $pdf->setSourceFile($file);
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// import a page
$templateId = $pdf->importPage($pageNo);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);
// create a page (landscape or portrait depending on the imported page size)
if ($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
}
// use the imported page
$pdf->useTemplate($templateId);
$pdf->SetFont('Helvetica');
$pdf->SetXY(5, 5);
$pdf->Write(8, 'Generated by FPDI');
}
}
The problem was the encryption in the pdf file, it was protected from changes without a password.
I used qpdf to export a decrypted version of the pdf as a temporary file. Then I used pdftk to join the files. Turns out to be far faster than the PHP libraries.