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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集