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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算