Merging of pdf in php - php

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

Related

PHP FPDI Try to add text in Hebrew language to pdf file but i got problem in utf

I use FPDI library for php and try to add text into pdf file.
It's work but when i change the text to Hebrew i got
×•× ̈×– הצלח×a×TM להוס×TM×£ × ̃×§×¡× ̃
This is my code:
<?php
use setasign\Fpdi\Fpdi;
use \setasign\Fpdi\PdfParser\StreamReader;
require_once('fpdf/fpdf.php');
require_once('fpdi/src/autoload.php');
$rawPostBody = file_get_contents('php://input');
$signatures = (array)json_decode($rawPostBody);
$fileURL = $signatures['filURL'];
$signaturesData = $signatures['signatures'];
// Initial Fpdi
$pdf = new Fpdi();
// Load external pdf file with StreamReader (We Use StreamReader Only for External file)
$fileContent = file_get_contents($fileURL,'rb');
$pdf->setSourceFile(StreamReader::createByString($fileContent));
// Get number of pages.
$pageCount = $pdf->setSourceFile(StreamReader::createByString($fileContent));
// Illiterate and create pdf pages.
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$tplId = $pdf->importPage($pageNumber);
$s = $pdf->getTemplatesize($tplId);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial','B',10);
$pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L', array($s['w'], $s['h'])); // This gets it the right dimensions
$pdf->useTemplate($tplId, null, null, null, null, true);
foreach ($signaturesData as $signa) {
if($signa->page == $pageNumber) {
$pdf->SetXY(number_format($signa->positionX), number_format($signa->positionY * 0.264583));
$pdf->Write(0, "שלום שלום");
}
}
}
$pdf->Output('pdf-with-value.pdf', 'D');
I read some post with a similar problem but I couldn't figure out what exactly I needed to do.
Thanks
I found solution, first I download hebrew font then i put the files in font folder then:
$pdf->AddFont('Rubik-Light', '', 'Rubik-Light.php');
$pdf->SetFont('Rubik-Light', '', 15);
$value = iconv('UTF-8', 'windows-1255', html_entity_decode('עובד פיקס'));
$value = strrev($value);
$pdf->Write(0, $value);

how to add images to multiple pages on pdf and download the full pdf

I started to work with fpdi with fpdf and I try to add more than one image to multiple pages and in the end, I want to download one PDF with the images over the PDF pages.
The problem is that always just the last PDF downloaded with the last page. Why I can't download one file with all the images?
foreach ($signatures as $signa) {
$fileContent = file_get_contents('http://www.africau.edu/images/default/sample.pdf','rb');
$pageCount = $pdf->setSourceFile(StreamReader::createByString($fileContent));
$pdf->setSourceFile(StreamReader::createByString($fileContent));
$tplId = $pdf->importPage($signa->page);
$pdf->useTemplate($tplId, 10, 10, 100);
$pdf->Image('signature.jpg', $signa->position->x, $signa->position->y, $signa->size->width, $signa->size->height);
if($signa->page === 2) {
$pdf->Output('D');
}
}
I found this Solution and its work for me.
Solution on my code:
$pdf = new Fpdi();
foreach ($signatures as $signa) {
$pdf->AddPage();
$fileContent = file_get_contents('http://www.africau.edu/images/default/sample.pdf','rb');
$pdf->setSourceFile(StreamReader::createByString($fileContent));
$tplId = $pdf->importPage($signa->page);
$pdf->useTemplate($tplId, 10, 10, 100);
$pdf->Image('signature.png', $signa->position->x, $signa->position->y, $signa->size->width, $signa->size->height);
}
$pdf->Output('newpdf1.pdf', 'D');

Trying to populate pdf template with user input

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.

How to overlay HTML generated PDF on top of existing PDF?

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

FPDF error: Template does not exist! fpdf

So I am trying to modify a pdf template with php.
I looked around and found out most people use FPDI and I gave it a try.
So What i did was
include('pdf/fpdf.php');
include('pdf/fpdi.php');
// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pagecount = $pdf->setSourceFile('pdf/menu_blanka.pdf');
// import page 1
$template = $pdf->importPage($pagecount);
// use the imported page as the template
$pdf->useTemplate($template, 0,0,0);
And I constantly get this error "FPDF error: Template does not exist!".
Can you guys help me out ?
Here is how I have achieved this in a project of mine. Works fine ..
require_once 'pdf/fpdf.php';
require_once 'pdf/fpdi.php';
$pdf = new FPDI();
$pdf->setSourceFile('/path/to/source.pdf');
$pdf->AddPage();
$tplidx = $pdf->ImportPage(1);
$pdf->useTemplate($tplidx, 0, 0, 0);
$output = $pdf->Output('output.pdf', "S");

Categories