linux, centos, php, word picture transfer method

At the beginning of the customer's job, really did not expect such a method to be stuck, really drunk, after a long week, and finally get this problem, the following came to introduce this method to the following . We can facilitate.

(Direct conversion could not, so to find a compromise, to turn into a pdf file, pdf and then turn pictures)

Let me talk about the installation environment (I also experienced several failures before summed up the installation environment, and * indicates a required action, if God is great, then please pass)

[Note] code section should be due to my own writing style of some invisible, use the mouse to drag what you can see

  1. Server environment: centos7
  2. * Integrated Environment: pagoda
  3. After installing pagoda, choose to install LNMP integrated environment (php preferably mounted version 5.6 and above)
  4. It should be noted here is that you must use nginx environment (do not use apache, pit too much), there is the use of integrated environment pagoda, do not ask me why, because too many pit, as long as sufficient time to work not long, you you can try to install their own to try a variety of environments (for fast, you follow my environmental installation steps away)
  5. wait. . .
  6. After the installation is complete environment, we enter the pagoda software store, click on the back of php settings, and then to install php imagemagick extension
  7.  Waiting for the php extensions of the installation is complete

  8. After the installation is complete, we begin to install the server libreoffice

  9. Directly execute the following code can be (a good first install ok, back when the two installation may be error, but as long as the first one installed, in fact, no big impact)
    1 yum install libreoffice
    2 yum install libreoffice-headless
    3 yum -y install  libreoffice-langpack-zh-Han*

     [Note] code section should be due to my own writing style of some invisible, use the mouse to drag what you can see

  10. After installation is complete libreoffice, we use the following code to test whether the installation was successful:
    soffice --headless --invisible --convert-to pdf 1.docx

     [Note] code section should be due to my own writing style of some invisible, use the mouse to drag what you can see

  11. 如果在执行命令的目录下,生成了一个与word同名的pdf文件,即说明libreioffice安装成功了
  12. 对于libreoffice的linux使用可以自行百度
  13. 在php中,我们可以使用 exec() 函数来调用命令行操作,还有 shell_exec() 等函数(函数用法自行百度)
  14. 如果在php方法中不能执行 exec() 方法,说明php将这些方法禁用了,这个时候我们可以修改 php.ini 文件中的配置
  15. 在php.ini 文件中找到 disable_functions = 开头的一行,然后在后面把自己要使用的函数删掉,然后重新启动一下php,然后我们写的php就可以调用 exec() 等函数了
  16. 至此,我们就把word转图片的所要用到的所有环境都安装成功了
  17. php中word转pdf的方法:
    $result = exec("soffice --headless --invisible --convert-to pdf 1.docx");
    echo json_encode($result);

    【注】代码部分应该是由于我自己写的样式影响的有的看不见,用鼠标拖一下就能看见
    可以在同目录生成word同名pdf文件

  18. php中pdf转图片的方法:
    $result = pdf2png("/www/wwwroot/wwj.wangwenjie.club/1.pdf","/www/wwwroot/wwj.wangwenjie.club/");
    
    function pdf2png($pdf, $path)
        {
            if (!extension_loaded('imagick')) {
                return false;
            }
            if (!file_exists($pdf)) {
                return false;
            }
            $im = new \Imagick();
            $im->setResolution(120, 120); //设置分辨率 值越大分辨率越高
            $im->setCompressionQuality(100);
            $im->readImage($pdf);
            foreach ($im as $k => $v) {
                $v->setImageFormat('png');
                $fileName = $path . md5($k . time()) . '.png';
                if ($v->writeImage($fileName) == true) {
                    $return[] = $fileName;
                }
            }
            return $return;
        }

    【注】代码部分应该是由于我自己写的样式影响的有的看不见,用鼠标拖一下就能看见
    可以在同目录下生成png图片(pdf几页就有几张图片生成)
    关于pdf转图片的方法大家可以参考:https://blog.csdn.net/jeff_love_marina/article/details/80838055
    (环境等上面的步骤已经都安装好了,所以直接看该链接的方法就行)

  19. 至此,在 linux 下 php 将 word 转图片的方法就整个说完了,加油吧

Guess you like

Origin www.cnblogs.com/shangguanbiji/p/12072499.html