PDF to JPG with PHP unexpected behaviour - php

I'm using Imagick to convert all pages in a PDF to JPGs. But something is off, probably because of my lack of PDF knowledge.
The problem
The PDF I'm converting is a book. All pages turns out great except the cover page/first page. If I open the pdf in a PDF previewer it looks as it should but my converted image displays the cover page + a backside of the book. If I open the first page of the PDF in Illustrator I get the same result as my image. I also have a problem with some pdf's containing margins which is not displayed in a PDF previewer but as whitespace in my images.
But when converted to an image, the backside is white. It's like it doesn't exist but the image dimensions equals both of them.
Cause and Solution
Why does this happen and can I ignore the back somehow? Or should this be fixed in the PDF? Shit in shit out...
Code example
//...
$imagick->readImage('book.pdf[0]');
//...
file_put_contents('cover.jpg', $imagick):
Possible solution
Seems like if I use trimImage I get the expected result. So I have a bonus question. How does trimImage work? How does it know what to trim, let's say for example if I have a white image with text in the middle?

I managed to solve this and the solution was really simple. The solution is to use cropbox. You can set Imagick options by using setOption.
$imagick->setOption('pdf:use-cropbox', 'true');
Available options can be found here: http://www.imagemagick.org/script/command-line-options.php

Related

How to create pdf like a example using fpdf

This is my PDF code below:
$this->SetFillColor(0);
$this->SetTextColor(255,255,255);
$this->Cell(90,10,$airline_name,'LRBT',0,'L',true);
$this->Cell('10',10,'','',0,'L',false);
$this->SetFillColor(255,255,255);
$this->SetTextColor(0);
$this->SetFont('Times','',10);
$this->MultiCell(90,5,$com_info,1,'L',false);
$this->Ln(0);
$this->SetY(23);
$this->Cell(30,10,"MAWB",'LTB',0,'L',false);
$this->Cell(60,10,$mawb,'TRB',0,'L',false);
$this->Cell(10,10,'','',0,'L',false);
$this->Cell(45,20,'This Pieces No.\n$pn','LR',0,'L',false);
$this->Cell(45,20,'Total Pieces\n$pn','R',0,'L',false);
$this->ln(0);
$this->SetY(36);
$this->Cell(30,10,"Booking_ref",'LTB',0,'L',false);
$this->Cell(60,10,$booking_ref,'TRB',0,'L',false);
$this->Cell(10,10,'','',0,'L',false);
$this->Cell(25,20,'HAWB','LTR',0,'L',false);
$this->Cell(65,20,$quotation_no,'TR',0,'L',false);
According to above code, the PDF is as shown below:
pdf image No.01
Basically, I want to do display it as shown here:
pdf image No.02
In the PDF, the data will come from the database.
I would like to display like pdf image No.02 But i have done like pdf image No.01.
How to get the pdf image like pdf image No.02.
How can I solve it? Please help.
Not overly familiar with fPDF. It looks OK for quick, simple stuff.
Have you tried using R&OS ezPDF Creator instead - not sure what state of development/maintenance it's in these days but I think it may give you more detailed control for the kind of output you want.
I've used it for years for bespoke PDF output from PHP.
Just checked and it looks like it's still being maintained - see:
http://sourceforge.net/projects/pdf-php/
Cheers,
Arthur
I have tried fpdf and a few other libraries for a big job i have done a couple of years back. You might want to look at dompdf, the library takes html and converts it into pdf flawlessly (after a couple of tries to get it right, but still) - only thing is, read the docs - images (like your barcode there) must be linked with server path, i.e. /path/to/image/image.jpg
Basically it looks ok. Its just a bunch of little things to adapt. e.g. setting the font to bold is done via $pdf->SetFont('Arial','B',14); before the output statement and setting it back to $pdf->SetFont('Arial','',14); afterwards. See fpdf documentation for details.

Using TCPDF to write text onto an image

I am brand new to the TCPDF library and after reading through the documentation I am not sure if it is possible to do what I need. I have an image file, lets call it background.tiff, and I want to place this image inside a new pdf and write some text ontop of this image in certain positions. I can't find any information or documentation on writing text onto an image, or even positioning text absolutely on the page. Is this something that should be done with writeHTML() instead? Ideally this pdf will be printed later - is it possible to 'outline' the text for printing purposes in TCPDF? Any help would be much appreciated, I am feeling a bit lost here.
edit: I am completely open to switching to another library if it would be a better solution.
Altough solution is already given in comment here's what u need to know:
Image() - read about inserting an image into PDF document.
SetX(), SetY(), SetXY(), GetX(), GetY() - read about setting/getting pointers. Keep in mind that PDF generation is pretty similar to printing process. If you generate some content, move pointer back over that content, and generate something new, it will appear on top of content u've 'printed' before.
Cell(), MultiCell(), Write() etc.
Oh, and if u want to be precise with generating PDFs try to avoid converting HTML to PDF. Use pointers instead. It's much more precise, works better with page breaks and u don't need to worry about CSS compatibility.

Can I make html table into image to embed into pdf

I have been reading about this but I have to do some thing. I have a table with diagonal text in the heading columns and horizontal text in the rows. I was able to make text diagonal using CSS rotation thing, table data is coming from db and I want to generate the same html into the pdf. I don't know who to write diagonal text in the pdf. The solution is to create an image of the visible html table with data and then store it and then make pdf and embed that image there. I have been reading the GD library but how can I make table in GD library I am stuck , Stack Overflow is the last resort. Please help.
try this ezpdf class http://www.ros.co.nz/pdf/readme.pdf. they got some sample code to rotate text and how to embed image into pdf.
example
for ($angle=0;$angle<360;$angle=$angle+20){
$r=rand(0,100)/100;
$g=rand(0,100)/100;
$b=rand(0,100)/100;
$pdf->setColor($r,$g,$b);
$pdf->addText(300+cos(deg2rad($angle))*40,300-
sin(deg2rad($angle))*40,20,$demotext,$angle);
{
$pdf->stream();
If you want to go directly from your HTML code to PDF, and if you need something entirely in PHP you can try dompdf. The 0.6.0 release will include CSS transform support.
For image generation in PHP you can use GD functions (fast) or iMagick (not so wide spread, docs are WorkInProgress, but you could do almost anything you can imagine with it).
GD should be enough.
A simple idea about making text diagonal would be to just rotate the image, once you output text from the db onto it.
Use wkhtmltoimage or wkhtmltopdf.
Do you need to do this only once or does this need to be an ondemand service? If not why not just load the html page take a screenshot and crop it down to the table?
Instead of trying to convert HTML directly to a PDF document, you might want to try a PDF library for creating the document directly in PHP. This way you will get more control over what the PDF looks like, and it may be a better solution that trying to convert your HTML output.
https://stackoverflow.com/questions/560583/which-is-the-best-pdf-library-for-php

Zend PDF image bad quality

I've tried Zend PDF and it works pretty well but I have a troublesome problem : when I add an image to a page with drawImage() it always appears pixilated regardless position and dimensions.
There's many lines I don't understand in the Zend images classes, has someone already encountered this problem?
How can I fix this ?
(I cannot post my code today but it's very simple and I think my question is not specific)
Remember that PDF is inherently a print medium (300dpi and higher), while your average .jpg is intended for screen viewing (72-100dpi). If you don't supply source images with approximately the same resolution as the document you're inserting it into, the PDF display engine will have to do all kinds of scaling to make things fit. Once when you insert the image to stretch it out to the size you want, and then downscaling again to make the PDF fit on your screen.

How rotate a pdf document using php?

How can I rotate a pdf document using php and linux?
Rotate an Entire PDF Document's Pages to 180 Degrees
$command = "pdftk in.pdf cat 1-endS output out.pdf";
system($command);
You could use pdf90 from PDFjam.
To address some of the other suggestions:
I would be wary of adjusting the Rotate attribute directly, as this attribute is stored as text, and '90' or '270' obviously uses a different number of bytes to '0'. I believe inserting the required bytes can make a mess of the index tables that appear at the end of a PDF file. After that, you're reliant on a viewer being able to interpret the damaged file.
Rendering the PDF to an image and rotating that is going to rasterize any text or vector graphics, leading to either a much larger file size, or much lower quality.
You would have to use a external library like this to extract the info a generate an image, then put it back to the pdf(or a new one)
EDIT:
If your going to get a Logo or a diagram this is a good choice, if its a big document with text and lots of images... its going to be pretty hard, could you edit the OP with more info on what you need?
You will have to access the PDF as a binary file then find and adjust the "Rotate" attribute for each page (and possibly the "MediaBox" attribute). I am not aware of any PDF libraries for PHP that allow for this sort of direct manipulation of existing files. This method will not require changing anything about the content of the pages, it just changes the orientation the pages are displayed in by viewers (similar to the EXIF Orientation information in JPEG images).
This snippet of perl should help illustrate what parts of the file you are looking for.
There are a few libraries for handling PDFs with PHP.
Here's a good code example using such a library. I found it, just by Googling "PHP PDF":
http://www.fpdf.org/en/script/script2.php

Categories