dsh1956
dsh1956
2016-01-14 20:20

PHP - 可以采用base64编码的pdf数据字符串并压缩它?

已采纳

So I have an XML file that has a base64 encoded data string for a pdf file, which just has an image taken from an iPad.

This pdf file can be excessively large, as much as 14MB with dimensions of 57"x38".

These images are taken from an iPad through a DocuSign session, thus I have no way at the moment of controlling their size or format before they get to my php listener script.

However, my script cannot work with such large files as my CRM's API file size max is 10MB, and I need a way of reducing the file size before I can upload it through my CRM's API.

Now if it was just a jpg, it would be ok as there are plenty of ways to reduce file size in PHP, but it is a PDF. I have found plenty of PHP extensions for making PDFs, but I haven't found any for reading a PDF and extracting an image from it.

So is there a way to extract the image from the PDF through PHP, or perhaps compress the pdf file?

UPDATE

I didn't think about the possibility of converting a pdf into a jpg, which apparently is easier to do with imagick. Having my server admin install it and I will see if I can make it work with my script.

UPDATE 2

So I was able to get imagick working and locally I am able to convert pdf files into jpg, and reduce file size dramatically.

However, I am running into an issue using it with my application. I get the following error from my CRM's API:

Failed to parse XML-RPC request: Invalid byte 1 of 1-byte UTF-8 sequence.

So the process is the following:

  1. XML file has a base64 encoded data stream of the pdf file.
  2. I decode this data
  3. I then convert with imagick and reduce file size
  4. I base64 encode and prep for upload

CODE

        $imageBlob = base64_decode((string)$pdf->PDFBytes);
        $imagick.$x = new Imagick();
        $imagick.$x->readImageBlob($imageBlob);
        $imagick.$x->setImageFormat('jpeg');
        $imagick.$x->setImageCompressionQuality(60);
        $imagick.$x->adaptiveResizeImage(1024,768,true);
        $imageBlob = $imagick.$x->getImageBlob();
        $PDFdata[] = base64_encode($imageBlob);

I can test the date by using the proper header and I can see the new jpeg fine, so I assume the data is properly formatted.

What I am missing?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqq3623 dqq3623 5年前

    Ok, so I figured it out.

    Imagick was the way to go, and my use of it was good. I just goofed up on the file name because I wasn't using a proper dynamic variable name. Code should have looked like this:

    CODE

    $imageBlob = base64_decode((string)$pdf->PDFBytes);
    ${'imagick'.$x} = new Imagick();
    ${'imagick'.$x}->readImageBlob($imageBlob);
    ${'imagick'.$x}->setImageFormat('jpeg');
    ${'imagick'.$x}->setImageCompressionQuality(60);
    ${'imagick'.$x}->adaptiveResizeImage(1024,768,true);
    $imageBlob = ${'imagick'.$x}->getImageBlob();
    $PDFdata[] = base64_encode($imageBlob);
    $PDFfile[] = $FormCustomField . $x . '.jpg';
    

    So the error I was getting was because of an invalid file name, because the $x variable in the previous code was getting junk values. Now everything works fine.

    点赞 评论 复制链接分享