Home > Article > Backend Development > How to implement the preview function of WeChat applet pdf files based on PHP
(Recommended tutorial: PHP video tutorial)
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.
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.
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.
After specifying the corresponding page number through parameter p, the picture of the corresponding page number will be displayed on the left .
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
You can also compile and install it, which will be omitted here for the time being. Yes.
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
echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig
<?php try{ $url = !empty($_GET['url']) ? $_GET['url'] : die('error'); $page = isset($_GET['p']) ? $_GET['p']-1 : '0'; $file_name = 'temp_' . mt_rand(1000,9999) . 'pdf'; file_put_contents( './'.$file_name,file_get_contents($url)); $imagePreview = new imagick('./'. $file_name . '['.$page.']'); $imagePreview->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!