Home>Article>Backend Development> How to implement the preview function of WeChat applet pdf files based on PHP

How to implement the preview function of WeChat applet pdf files based on PHP

WBOY
WBOY forward
2022-08-26 14:05:28 2966browse

(Recommended tutorial:PHP video tutorial)

Problem description

WeChat applet of library class some time ago I encountered a problem during development, which was to preview PDF files in Alibaba Cloud OSS in a mini program. There is only one official solution provided by WeChat, which is to cache the document locally and then open it with the resource manager.

The problem with writing this way is obvious. If the PDF file is small, it will be fine. When the PDF file is large, the loading speed will be very slow. And I only need to preview the first few pages, and I also need to cache all the documents and open them in the file explorer. Very bad experience.

Exploration process

  • Option 1: Use the webview of the mini program to preview on the web page. The experience is still okay, but the problem encountered is that the cross-domain problem cannot be solved.

  • Option 2: pdf.js. Many solutions on the Internet say to use this and write a static page on the server for preview. With this solution, it's okay if the PDF file is on the server of your own domain name. However, if the file is in a third-party oss, you will still encounter cross-domain problems. Moreover, the online mini program will issue a jump prompt. The pdf.js document is difficult to read, with various compatibility issues and unknown problems that are unpredictable.

  • Option 3: Convert pdf files to images, and the mini program only loads the corresponding images. This solution is the most perfect, but it is more suitable for postgraduate back-end developers. Our backend is too troublesome, so I have no choice but to fix it. I can only do it myself.

Implementation Idea

Pass the link of the pdf file to be previewed and the number of pages to be previewed to the backend. The backend parses the pdf file according to the passed parameters and returns the required page as a picture.

  • The server needs to install image processing software (ImageMagick) for generating images
  • php needs to install the extension corresponding to ImageMagick
  • The server needs to install ghostscript for opening and parsing pdf file

Final effect

After specifying the corresponding page number through parameter p, the picture of the corresponding page number will be displayed on the left .

Implementation steps

  • Download, compile and install ImageMagick
wget https://netix.dl.sourceforge.net/project/imagemagick/im7-src/ImageMagick-7.0.7-25.tar.gz tar -zxvf ImageMagick-7.0.7-25.tar.gz cd ImageMagick-7.0.7-25 ./configure make && make install

The corresponding operations of the five commands are: download, extract, open directory, configure, compile and install

  • Install the extension imagemagick for php (I installed it with one click in the pagoda)

You can also compile and install it, which will be omitted here for the time being. Yes.

  • Install ghostscript
wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs925/ghostscript-9.25.tar.gz tar zxvf ghostscript-9.25.tar.gz cd ghostscript-9.25 ./configure --prefix=/usr make && make install

The corresponding operations of the five commands are: download, decompress, open the directory, configure, compile and install

  • Configure the commands into linux
echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig
  • php file parsing code
setImageFormat( "jpg" ); // $imagePreview->setResolution( 900, 900 ); header( "Content-Type: image/jpeg" ); echo $imagePreview; unlink('./'. $file_name); }catch(\Exception $e){ echo '发生错误'; }

Note: The path of the pdf file can be your own server, or you can adjust it yourself.

(Recommended tutorial:PHP video tutorial)

The above is the detailed content of How to implement the preview function of WeChat applet pdf files based on PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete