PHP Imagick annotateImage fails on some images - php

I have a PHP script which generates images from PDF pages using PHP Imagick. In which i write a piece of data on top of the image using annotateImage function and take it as output. This is failing recently on some PDF images being selected. I mean when a specific PDF is being given as input to this program the annotateImage function fails to write data where on many other PDF files it is performing well...
this is the following code which run on a loop to generate this images...
$imagick->setResolution(200,200);
$imagick->readImage('files/pdffile.pdf[1]');
$imagick->annotateImage($draw_small, 1250, 100, 10, 'Hello header');
$imagick = $imagick->flattenImages();
$file = fopen("/outputfile.jpg", 'w+');
chmod($folder_in_action."/outputfile.jpg", 0755);
fclose($file);
$imagick->writeImage('/outputfile.jpg');
$imagick->clear();
The code works well and get the exact PDF page as image in the folder but the annotateImage does not works on specific PDF files and so the output comes with no 'Hello header' message.
The same code works perfect on most of the PDF's where i get the specific PDF page with 'Hello header' message written on top as image...
So could not able to guess what could be the issue...
PDF file which does not allow annotateImage to write text is here:
http://wikisend.com/download/143652/notworking.pdf
PDF which is working (usually most of the pdf files are working properly...)
http://wikisend.com/download/215044/working.pdf

Related

PHP: Imagick converting multi-page pdf into single image issue

I am trying to convert a multi-page pdf into a single image with all pages in the pdf stacked. I am trying to use the Imagick() class. I found similar questions but none have helped the issue. Here is the code I am using.
$img = new Imagick();
$img->setResolution(300, 300);
$img->readImage(example.pdf);
$img->resetIterator();
$imga = $img->appendImages(true);
$imga->setImageFormat('jpg');
$imga->writeImage("pdfs.jpg");
Only the last page of the pdf is saved in the jpg file the other pages are not.
I am using ImageMagick-7.0.10, PHP 7.3.12 and I am on Windows 10.
You,have to add the index of the pdf page you want.
For the first page :
$img->readImage(example.pdf[0]);
I'm looking to generate animated gif with multipage pdf... not done yet !

PDF Image thumbnail preview in laravel 5.1?

I have file module, where I want to list all files,with thumbnail preview.
as like follows,
I am storing files in storage folder,which is not accessible via http.
So How can I provide thumbnail preview for docs especially image and PDF.
Is there any package availabe in laravel 5.1?
The preview image of a PDF file is usually the first page of the PDF file.
You can use ImageMagick to obtain that first page of the PDF file.
<?php
$imagick = new imagick('sample.pdf[0]'); // 0 specifies the first page of the pdf
$imagick->setImageFormat('jpg'); // set the format of the output image
header('Content-Type: image/jpeg'); // set the header for the browser to understand
echo $imagick; // display the image
?>
You can also output (save the contents of the image in a file) and store it under a thumbnail folder with the PDF name as the file name. like ( sample.jpg )
As for a solution based on laravel, I don't think laravel has any package that can do the same
Though the question was posted quite a long ago but I'm answering because it might helps others. There is a package for generating image from pdf. Pdf to Image. You should have Imagick and Ghostscript installed

PDF image generating with PHP

I have a PHP application which generates PDF files that may contain images. When generating the PDF file I can't open it because it is corrupted or not PDF format. If I omit the image everything works fine. Long ago this thing worked nicely I don't remember if I changed anything about the handling of images. I use small images so not the imagesize is what causes the problem. Here is my code:
if($cell['type'] == "custom_image"){ // Uploaded image
$imgxploded = explode('.', $cell['img']);
$imgext = end($imgxploded);
$imgfile = $root_directory.'uploads/'.$cell['img'];
$this->Image($imgfile, $blockInternalX, $blockInternalY, $cell['width'], $cell['height'], strtoupper($imgext));
}
Any ideas about what goes wrong?
edit: I use FPDF class to manage/produce PDF files

Converting all pages of a multi page PDF into an image with PHP causes a duplicate result in the image

So I am using imagick to convert a PDF into an image. All works well for single page PDFs but I ran into a snag with a multi page pdf.
I found an example on how to deal with multiple pages here: http://php.net/manual/en/function.imagick-appendimages.php so I took that code:
$im = new Imagick($src);
$im->readImage($src);
$im->resetIterator();
$ima = $im->appendImages(true);
$ima->setImageFormat('jpg');
header("Content-Type: image/jpeg");
print $ima;
This works in that it produces an image with each page of the pdf the problem is that the PDF is displayed twice in the image. So, a three page PDF is displayed as a single image of 6 pages.
Here is an example PDF
and the resulting image created with the above code.
What am I doing that is causing this to happen?
Resolution:
Based on tandu's answer this did the trick
$im = new Imagick($src);
$im->resetIterator();
$ima = $im->appendImages(true);
$ima->setImageFormat('jpg');
header("Content-Type: image/jpeg");
print $ima;
The Imagick constructor loads the image from the file you provide. When you run readImage(), it loads them again. You only need one of those two.

Bloated PDF created by TCPDF

In a web app developed in PHP we are generating Quotations and Invoices (which are very simple and of single page) using TCPDF lib.
The lib is working just great but it seems to generate very large PDF files. For example in our case it is generating PDF files as large as 4 MB (+/- a few KB).
How to reduce this bloating of PDF files generated by TCPDF?
Here is code snippet that I am using
ob_start();
include('quote_view_bag_pdf.php'); //This file is valid HTML file with PHP code to insert data from DB
$quote = ob_get_contents(); //Capture the content of 'quote_view_bag_pdf.php' file and store in variable
ob_end_clean();
//Code to generate PDF file for this Quote
//This line is to fix a few errors in tcpdf
$k_path_url='';
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF();
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// add a page
$pdf->AddPage();
// print html formated text
$pdf->writeHtml($quote, true, 0, true, 0); //Insert Variables contents here.
//Build Out File Name
$pdf_out_file = "pdf/Quote_".$_POST['quote_id']."_.pdf";
//Close and output PDF document
$pdf->Output($pdf_out_file, 'F');
$pdf->Output($pdf_out_file, 'I');
///////////////
enter code here
Hope this code fragment will give some idea?
You need to see what it is putting inside the PDF. Is it embedding lots of images or fonts?
You can examine the contents with lots of PDFtools. If you have Acrobat 9.0, there is a blog article showing how to do this at http://pdf.jpedal.org/java-pdf-blog/bid/10479/Viewing-PDF-objects
Finally I have managed to solve the problem.
The problem was that by mistake I had inserted a link to email id in the web page that was getting rendered to PDF. By just removing this link the size of the generated PDF went down to just 260 kb!
Thanks everyone who tried to help me out in solving this problem.
Current TCPDF version now includes font subsetting by default to dramatically reduce PDF size.
Check the TCPDF website at http://www.tcpdf.org and consult the official forum for further information.

Categories