Page 1 of 1

PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-14T04:28:26-07:00
by rezapilot
Version: ImageMagick-6.9.2-0

We have deveopers who is using a linux server on amazon AWS.

We want to convert PDF to .jpg for presentation.
It works fine for most PDF´s, but when we upload a PDF with multiple pages, the conversion breaks.

The settings I have for the "convert" part are:
$max_width = '842';
$max_height = '595';

//$attachments_array = array();
//exec("convert -density 300 -trim ".$attachment_path." -quality 30 ".$pdfImgPath);
//exec("convert -version -density -1 ".$attachment_path." -quality 100 ".$pdfImgPath);
//exec("convert -density 1080 -set units PixelsPerInch -density 1080 -quality 300 -background white -flatten -resize {$max_width}x{$max_height} ".$attachment_path." ".$pdfImgPath);
exec("convert -density 1080 -set units PixelsPerInch -density 152 -quality 100 -background white -flatten -resize {$max_width}x{$max_height} ".$attachment_path." ".$pdfImgPath);

// iterate over pages of the pdf file

The pdf itself is created in PPT, and in landscape mode, and I guess the resolution for A4 landscape is 842x595 using 72 dpi.
I have tried to increase quality to 100, to make the image crisp.
and the second density to 152.

When uploading a PDF from 23Kb to 8Mb it all works fine.
but when a small PDF is creted with multiple "pages" the upload breaks.

Re: PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-14T05:28:33-07:00
by snibgo
rezapilot wrote:... but when we upload a PDF with multiple pages, the conversion breaks.
Define "breaks". What happens? Do you get any images? Any error messages? Does your computer catch fire?

Your commands have bad syntax, for example:

Code: Select all

convert -density 1080 -set units PixelsPerInch -density 152 -quality 100 -background white -flatten -resize {$max_width}x{$max_height} ".$attachment_path." ".$pdfImgPath
You should read the input, process it, and write the output(s). For example, resize after you have read the image.

How large are your PDF pages (in pixels) and how many pages? Do you have enough memory?

Re: PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-14T05:52:45-07:00
by rezapilot
breaks: in the backend I can see the image like this: http://www.underconsideration.com/brand ... d_base.png

the file size, themself are not the issue since I have managed to upload files up to 18Mb.
The "break" issue occurs when the PDF file has more than 1 page.

Re: PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-14T06:10:54-07:00
by snibgo
So, you get an image. What is wrong with it?

I asked "How large are your PDF pages (in pixels) and how many pages?" Not megabytes, but pixels and pages.

I suggest you narrow down the problem, to get a failure with one PDF file and one command without any variables. Tell us what that command is, put the PDF somewhere like dropbox.com and paste the URL here.

Re: PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-14T06:39:10-07:00
by rezapilot
"So, you get an image. What is wrong with it?"
But I cannot use the converted image in my portal since it does not convert properly.

the pixle size for 1 page PDF is 960x718 when using 72 DPI. 1
the issue/error with the broken image occurs ONLY when the PDF has more than 1 page.

here is the link to the heavy PDF: https://www.dropbox.com/s/w91znlnctv59k ... 2.pdf?dl=0
and the PDF w 2 pages: https://www.dropbox.com/s/juaq8qz9cwijz ... e.pdf?dl=0

Re: PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-14T07:17:31-07:00
by snibgo
What command fails with one of those PDF files?

Re: PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-15T00:01:53-07:00
by rezapilot
@snibgo, I appreciate your help alot, but the situation is that the developers that we work with have "left".

Background:


The platform is based on wordpress, the idea is that "admin", uploads a PDF, and using imagemagick, we convert it to a jpg, for the final client to view, using lightbox popup.

While uploading PDF´s that are only 1 page, everything works fine, the image gets uploaded and the final client can see it in the lightbox. :)

But when admin uploads a PDF that has more than 1 page, thats when in wp-admin the image breaks, as I tried to discribe it last time.
And final client is not able to see or use the jpg image in lightbox, since it does not convert to jpg.

They have created the function of converting multiple PDF´s as custom work, and the script that I can find in the php file is as below.
This should be the one that converts and puts the PDF in page 1-infinite.

Code: Select all

 // iterate over pages of the pdf file
    
    $pno = 0;
    for($p = 1; $p <= $pages; $p++){
        $im->setIteratorIndex( $p - 1 );
        //$im->setResolution(240, 350);
        $im->setImageFormat('jpg');


        $filename_neu = $filename_wo_extension .'_'. $p .'.jpg';  
        $attachment_title_final = $attachment_title."-".$pno;
        $attachment_title_final = str_replace(" ", "-", $attachment_title_final);   

        if($pages > 1):

        $pdfImgPath = $upload_dir['basedir'].'/pdf_images/'.$attachment_title_final.'.jpg';
        $pdfImgUrl = $upload_dir['baseurl'].'/pdf_images/'.$attachment_title_final.'.jpg';

        endif;

        $arrData = array(
              'attachment_id' => $post_ID,
              'img_path' => $pdfImgPath,
              'img_url' => $pdfImgUrl
              );
        //pr($arrData);
        //die();
        $isInsert = $wpdb->insert($strTbl , $arrData);
        $pno++;
    }
  endif;    
    
  return $post_ID;
}

add_filter('add_attachment', 'insert_luminance_datas', 10, 2);

//get pdf name by id
When it comes to your points that the code has bad syntex, I would really be glad if you could tell me besides the order, what specific code I can insert.
the command that I use now is: (the same as before)

Code: Select all

 convert -density 1080 -set units PixelsPerInch -density 152 -quality 100 -background white -flatten -resize {$max_width}x{$max_height} ".$attachment_path." ".$pdfImgPath
The page is still in "testmode" ie, I can do changes on the php file and then try it out.

Re: PDF to -jpg convertion , the image breaks for multiple PDF

Posted: 2016-04-15T02:55:37-07:00
by snibgo
Sorry, I can't debug your code.