I'm currently using TCPDI merge four documents into a single PDF and temporarily storing the document using a variable. Is it possible to add "Bates Numbering" to the file, starting with the third page? (The first two pages are a cover letter.) Thanks in advance for pointing me in the right direction
require_once('../tcpdf/tcpdf.php');
require_once('../tcpdf/tcpdi.php');
// Create new PDF document.
$pdf = new TCPDI();
// iterate through the files
foreach ($filesarray 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);
// add a page with the same orientation and size
$pdf->AddPage($size['orientation'], $size);
// Set page boxes from imported page 1.
$pdf->setPageFormatFromTemplatePage($pageNo, $size['orientation']);
// use the imported page
$pdf->useTemplate($templateId);
}
}
// Output the new PDF
$attachment = $pdf->Output("Merged.pdf", "S");
I'm not familiar with Bates System but what i did was add the Page number as a Label and check your PageNo variable/index to determine when to show your batesNo.
For the labeling. See the TCPDF documentation.
*Code not tested
<?php
// iterate through the files
foreach ($filesarray AS $file) {
// get the page count
$pageCount = $pdf->setSourceFile($file);
$batesNo = 0000000001; //initialize*****
// 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);
/***********NEW BLOCK*******/
if ($pageNo > 3) {
$pdf->SetTitle('JonesNo-'.$batesNo);
} else {
$pdf->SetTitle($pageNo);
}
////////////////////////
// add a page with the same orientation and size
$pdf->AddPage($size['orientation'], $size);
// Set page boxes from imported page 1.
$pdf->setPageFormatFromTemplatePage($pageNo, $size['orientation']);
// use the imported page
$pdf->useTemplate($templateId);
}
}
?>
Related
i m Wana Delete One page from PDF By imagemagick , How can this be done? this my code this return just one page in pdf ? what the problem ?
$image = new \Imagick(__DIR__.'/test.pdf');
$pageNumber = $image->count();
$page = true;
$imgs = [];
for($i=0 ; $i<$pageNumber ; $i++){
$image->readImage(__DIR__.'/test.pdf['.$i.']');
if($i === 2 && $page == true){
$image->removeImage();
$page = false;
continue;
}
// $imgs[] = __DIR__.'images'.$i.'.jpg';
}
$image->setImageFormat("pdf");
$image->writeImage('images.pdf');
file_put_contents(__DIR__.'images'.$i.'.pdf',$image);
Assuming that the original pdf is having 5 pages (known as fivepage.pdf), then you may use the following steps to remove page 3 from it and generate a new pdf known as combined.pdf
use Imagick to open the pdf
determine the number of pages
loop over each page of this pdf
save the pages into separate , temporary jpeg files
push the jpeg files (except page 3, $i==2) into array
use the array to generate the "combined.pdf"
delete all the temp jpeg files
So the code is:
<?php
$file="./fivepage.pdf";
$im = new Imagick($file);
$resultimages = array();
$noOfPagesInPDF = $im->getNumberImages();
// loop over all the pdf pages
for ($i = 0; $i < $noOfPagesInPDF; $i++) {
$url = $file.'['.$i.']';
$image = new Imagick();
// $image->setResolution(300,300);
// use the above line if you want higher resolution
$image->readimage($url);
$image->setImageFormat("jpg");
$image->writeImage("./temp_".($i+1).'.jpg');
// include all pages except page 3 ($i==2)
if ($i!=2) {
array_push($resultimages, "./temp_".($i+1).'.jpg');
}
}
// generate the resulting pdf (omitting page 3)
$pdf = new Imagick($resultimages);
$pdf->setImageFormat('pdf');
$pdf->writeImages('combined.pdf', true);
// clear temp images
for ($i = 0; $i < $noOfPagesInPDF; $i++) {
unlink("./temp_".($i+1).'.jpg');
}
echo "pdf without page 3 saved";
?>
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.
i have a pdf upload script with php, my question is when user uploads a multipage pdf file , i want this to split in to individual pdf's. for instance if the pdf has 3 pages, the result should be 1.pdf , 2.pdf, 3.pdf etc.
for example
convert -density 300 filename.pdf filename.png works fine creating a png file , but i want the same in to pdf files.
You can use a FPDF and FPDI to do this.
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
// get the page count
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($pdfFilePath);
// iterate through all pages
for($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++)
{
// create blank document
$pdf = new FPDI();
// import a page
$pdf->setSourceFile($pdfFilePath);
$templateId = $pdf->importPage($pageNumber);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);
// create a page (landscape or portrait depending on the page being imported)
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);
// write the PDF file
$pdf->Output(('/path/to/save/'.$pageNumber.'pdf'), 'F');
}
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.
I have the following php function below that's converting a local PDF file into images. In short, I want each PDF page to be converted to a separate image.
The function converts the PDF to an image - but only the last page. I want every page of the PDF to be converted to a image and numbered. Not just the last page of the PDF.
Currently, this function converts the last page of example.pdf to example-0.jpg. Issue I'm sure lies within the for method. What am I missing?
$file_name = 'example.pdf'; // using just for this example, I pull $file_name from another function
function _create_preview_images($file_name) {
// Strip document extension
$file_name = basename($file_name, '.pdf');
// Convert this document
// Each page to single image
$img = new imagick('uploads/'.$file_name.'.pdf');
// Set background color and flatten
// Prevents black background on objects with transparency
$img->setImageBackgroundColor('white');
$img = $img->flattenImages();
// Set image resolution
// Determine num of pages
$img->setResolution(300,300);
$num_pages = $img->getNumberImages();
// Compress Image Quality
$img->setImageCompressionQuality(100);
// Convert PDF pages to images
for($i = 0;$i < $num_pages; $i++) {
// Set iterator postion
$img->setIteratorIndex($i);
// Set image format
$img->setImageFormat('jpeg');
// Write Images to temp 'upload' folder
$img->writeImage('uploads/'.$file_name.'-'.$i.'.jpg');
}
$img->destroy();
}
Seems like most of my code was correct. The issue was, I was using $img->flattenImages(); incorrectly. This merges a sequence of images into one image. Much like how Photoshop flattens all visible layers into an image when exporting a jpg.
I removed the above line and the individual files were written as expected.
/* convert pdf file to list image files */
if($_FILES['file_any']['type']=='application/pdf'){
$file_name = str_replace(substr($url,0,strpos($url,$_FILES['file_any']['name'])),'',$url);
$basename = substr($file_name,0,strpos($file_name,'.'));
$abcd = wp_upload_dir();
$delpath = $abcd['path'];
$savepath = $abcd['url'];
$dirpath = substr($savepath,(strpos($savepath,'/upl')+1));
$file_name = basename($file_name, '.pdf');
$img = new imagick($delpath.'/'.$file_name.'.pdf');
$img->setImageBackgroundColor('white');
$img->setResolution(300,300);
$num_pages = $img->getNumberImages();
$img->setImageCompressionQuality(100);
$imageurl = NULL;
$imagedelurl = NULL;
for($i = 0;$i < $num_pages; $i++) {
$imageurl[]=$savepath.'/'.$basename.'-'.$i.'.jpg';
$imagedelurl[] = $delpath.'/'.$basename.'-'.$i.'.jpg';
// Set iterator postion
$img->setIteratorIndex($i);
// Set image format
$img->setImageFormat('jpeg');
// Write Images to temp 'upload' folder
$img->writeImage($delpath.'/'.$file_name.'-'.$i.'.jpg');
}
$img->destroy();
}
There is a much easier way without the loop, just use $img->writeImages($filename,false); and it will make a file per PDF-page. As you said, if you flatten the image first, it only saves 1 page.
first install
imagemagick
in your system or server
and then create
pdfimage
folder and put pdf file in this folder then run the code and upload it file
<?php
$file_name = $_FILES['pdfupload']['name']; // using just for this example, I pull $file_name from another function
//echo strpos($file_name,'.pdf');
$basename = substr($file_name,0,strpos($file_name,'.'));
//echo $_FILES['pdfupload']['type'];
//if (isset($_POST['submit'])){
if($_FILES['pdfupload']['type']=='application/pdf'){
// Strip document extension
$file_name = basename($file_name, '.pdf');
// Convert this document
// Each page to single image
$img = new imagick('pdfimage/'.$file_name.'.pdf');
// Set background color and flatten
// Prevents black background on objects with transparency
$img->setImageBackgroundColor('white');
//$img = $img->flattenImages();
// Set image resolution
// Determine num of pages
$img->setResolution(300,300);
$num_pages = $img->getNumberImages();
// Compress Image Quality
$img->setImageCompressionQuality(100);
$images = NULL;
// Convert PDF pages to images
for($i = 0;$i < $num_pages; $i++) {
$images[]=$basename.'-'.$i.'.jpg';
// Set iterator postion
$img->setIteratorIndex($i);
// Set image format
$img->setImageFormat('jpeg');
// Write Images to temp 'upload' folder
$img->writeImage('pdfimage/'.$file_name.'-'.$i.'.jpg');
}
echo "<pre>";
print_r($images);
$img->destroy();
}
//}
?>