How to create PDF documents from image files, using PHP - php

With PHP application, I have to generate single PDF documents, from set of images.
Which is best way to achieve this? Can I use TCPDF library, and can you give me some example?

The easiest way is to use TCPDF (http://www.tcpdf.org) and set the images as full background as on the example n. 51.
For example:
// disable auto-page-break
$pdf->SetAutoPageBreak(false, 0);
// set image 1
$pdf->AddPage();
$this->Image('image_demo1.jpg', 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
$this->setPageMark();
// set image 2
$pdf->AddPage();
$this->Image('image_demo2.jpg', 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
$this->setPageMark();
// ...

you can use dompdf.You can find a copy of the original documentation at this link

I don't know your requirements but ImageMagick could probably do the trick. I believe that they also have a php wrapper available for all the image manipulation/conversion functions.

Related

tcpdf: add some space after writeHTML

I write some lines in TCPDF with
$this->writeHTML('<p>blabla</p>', true, 0, true, true);
$this->writeHTML('<p>blabla</p>', true, 0, true, true);
What can I do to add some space between the two blocks? I tried
$this->writeHTML('<p style="margin-bottom:10px">blabla</p>', true, 0, true, true);
$this->writeHTML('<p>blabla</p>', true, 0, true, true);
but as I read later TCPDF can not translate margin-bottom. I could use Cells instead but I think then I would have to calculate their heights and so on to put them on the correct position, right?
Any suggestions?

Internal linking in tcpdf

I am using TCPDF to create simple pdf document.
I am creating a page and adding link using below code
$pdf->addTOCPage();
$link = $pdf->AddLink();
$pdf->SetLink($link, 0, -1);
Now link is set successfull.But to navigate to that page what should I add ?
I tried below code , but it does nothing,
Return to TOC
// Create a fixed link to the first page using the * character
$index_link = $pdf->AddLink();
$pdf->SetLink($index_link, 0, '*1');
$pdf->Cell(0, 10, 'Link to INDEX', 0, 1, 'R', false, $index_link);
http://www.tcpdf.org/examples/example_045.phps
update -
refer to this function addHtmlLink() in tcpdf library.
You can add a internal link through this
$pdf->addHtmlLink('#'.$index_link, 'hello');
where 'hello' begin the name of anchor and and first param being identifier to the link.
In your case
$pdf->addHtmlLink('#'.$link, 'Whatever you like to name it');
$html = 'link name';
$pdf->writeHTML($html, true, false, true, false, '');

TCPDF getAliasNbPages - Get total pages of produced file

I'm using the follwing TCPDF code to generate PDFs with the writeHTML function. I have the page number footer function which puts page numbers at the bottom of each page as the pdf total pages grows. I'm trying to find a way at the end of creating all the pages to determine how many total pages the produced document has so that i can store that information into a variable and submit that data to a DB.
I've tried:
$total = $pdf->getAliasNbPages();
but doesnt work, Any ideas?
Thanks
// PAGE NUMBERED FOOTER
class MYPDF extends TCPDF {
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('Calibri', '', 8);
// Page number
$pageNumbers = 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages();
$this->Cell(0, 10, $pageNumbers, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
$html = 'html content';
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('file.pdf', 'I');
Use the getNumPages function instead:
$total = $pdf->getNumPages();
Note that this counts the amount of pages that have been created so far using the Addpage() function. So if you want the total amount of pages declare it after your last use of Addpage()
$pdf->Cell(0, 10,'{:ptp:}', 0, false, 'C', 0, '', 0, false, 'T', 'M');
To get the total number of pages, use $this->getAliasNbPages() as chown in this example https://tcpdf.org/examples/example_003/

right align logo in tcpdf header

I have the following tcdpf syntax in my php document:
$title="My Heading";
$obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.$title, PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
The logo appears on the left, how can I right align the logo and left align $title?
You must define custom header to align image.
This example
shows how to do this.
To align your logo right, you must set palign attribute to 'R' in image definition.
$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, 'R', false, false, 0, false, false, false);
Note that, your header data string position may not be what you want as. You will probably
need to play with several attributes, but in basic, image alignment can be done only via
overriding default Header() function as far as I know.
Check also Image() function.

tcpdf imagebox and cropping

I would like to add a 'imagebox' a box which contains the image and crops exceeding image value that is outside of this box. something like this:
I am not sure on how to do this if it's even possible.
Actually you can do this with Clipping.
The below line would show a photo of 200X300:
$pdf->Image('photo.JPG', 100, 100, 200, 300, '', true, '', false, 300);
To clip it you need:
$pdf->StartTransform();
$pdf->Rect(100, 100, 200, 300, 'CNZ'); //Clipping mask (CNZ style makes your day)
$pdf->Image('photo.JPG', 50, 50, 300, 400, '', true, '', false, 300);
//this would actually cut off a 50 units a in each direction.
$pdf->StopTransform();
You could crop an image with php , store it as a temp_file pass it to tcpfd and then delete it after the rendering of the pdf was done . Another option would be to use html/css to position a html element over the image but as we all know tcpdf doesn't know too much about css so i don't know if it will work .

Categories