dsh1956 2016-01-14 20:20
浏览 87
已采纳

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 2016-01-15 16:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏