PDF solution (3) -- Convert PDF to SwF

Source: Internet
Author: User
Related Topics

PDF solution (1) -- File Upload

PDF solution (2) -- convert files to PDF

PDF solution (3) -- Convert PDF to SwF

The previous article describes how to convert an uploaded file to a PDF file. It mainly describes how to convert a PDF file to a PDF file, prepare for the next step of online file browsing;

The following methods are used to browse PDF files online:

1. Ghost browser plug-in

This method relies on the browser plug-ins provided by the PDF Reader vendor. Mainstream PDF readers such as Adobe and Fu Xin install such controls when installing local clients, drag the local PDF file to the browser to see the effect as follows:

 

As shown in, the plug-in functions of different readers vary greatly, and different browsers have different effects, which cannot guarantee a uniform display effect. In addition, the above software must be installed on the client, has great limitations;

2. Use the jquery plug-in

Most of these methods rely on HTML5 technology. Given the current proportion of the browser market in China, this method is very small and will not be introduced here. For more information, see: 8 practical jquery plug-ins for online pdf file browsing

3. Implemented by converting to SwF

Although HTML5 technology is emerging, Flash is still widely supported. The flash control ensures the same Display Effect of Different browsers, some open-source front-end controls also provide a good SWF online browsing experience, so we will focus on this implementation method here;

Convert PDF to SwF

Swftools, an open-source software, is used to convert PDF files, images, and sounds to SWF files. It also provides Windows and Linux versions and can be deployed in a Linux environment;

Swftools: http://www.swftools.org/download.html

After downloading and installing the SDK, you can call it through the command line. ipv2swf provides many parameters for configuration, such as-P setting to open the PDF password, -Z uses the zlib compression mechanism of Flash 6, and-s sets more information parameters (you can obtain more detailed parameter information through ipv2swf-s help ), -O output SWF file location and so on:

 

The following code uses the Java code to start the system process to call the cmd2swf command for conversion. Java returns the result after the process is started successfully. In fact, we want to wait until the program returns the result after the file is converted successfully, in this case, you need to useProcessClass. In the Java documentation, we mention "processbuilder. the START () and runtime.exe C methods create a local process and return an instance of the Process subclass. This instance can be used to control the process and obtain relevant information ", with this, we can get the file conversion information and let the program return after the system process ends. The dealwith method in the Code isProcess. The detailed conversion code is as follows:

/*** Convert PDF to SwF * @ Param pdfpath PDF file path * @ throws exception */public static Boolean convert2swf (string pdfpath) throws exception {string swffile = pdfpath. substring (0, pdfpath. lastindexof (". ") + ". SWF "; file OUTFILE = new file (swffile); If (OUTFILE. exists () {return true;} file pdffile = new file (pdfpath); If (! Pdffile. exists () {return false;} // create a list that calls the swftools command list <string> command = new arraylist <string> (); command. add ("C :\\ swftools \ 20.2swf"); // The command path of cmd2swf is command. add ("-z"); command. add ("-s"); command. add ("flashversion = 9"); command. add ("-s"); command. add ("languagedir = C :\\ xpdf \ xpdf-Chinese-simplified"); // Add xpdf to solve Character Set problems during conversion command. add (pdfpath); command. add ("-o"); command. add (swffile);/*** Java startup When a system process is started successfully, the system returns the result directly without waiting for the execution of the system process to complete, here we need to wait for the system process to finish calling and then return the Java method */try {// create the system process processbuilder = new processbuilder (); processbuilder. command (command); // set the system program to be executed by the system process and the parameter process = processbuilder. start (); // use the attributes of this Process Generator to start a new process dealwith (process); try {process. waitfor (); // wait until the child process ends. The child process is the new process called by the system to convert the file} catch (interruptedexception e) {Throw new exception (E. getmessage () ;}} catc H (ioexception e) {Throw new exception (E. getmessage ();} return true ;} /*** process the process Io to prevent blocking or deadlock. * @ Param Pro * @ throws */Private Static void dealwith (final process Pro) {// The following figure shows how to handle congestion. Try {// start a separate thread to clear the pro. new thread () {public void run () {bufferedreader br1 = new bufferedreader (New inputstreamreader (Pro. getinputstream (); try {string text; while (text = br1.readline ())! = NULL) {system. out. println (text) ;}} catch (ioexception e) {e. printstacktrace ();}}}. start ();} catch (exception e) {e. printstacktrace ();} Try {// do not forget the error message generated when processing the result, otherwise the new thread () {public void run () will be blocked () {bufferedreader Br2 = new bufferedreader (New inputstreamreader (Pro. geterrorstream (); string text; try {While (text = br2.readline ())! = NULL) {system. err. println (text) ;}} catch (ioexception e) {e. printstacktrace ();}}}. start ();} catch (exception e) {e. printstacktrace ();}}

 

In the above Code, the parameter-s languagedir = c: \ xpdf-Chinese-Simplified is used when swftools command is created. The address here corresponds to another tool xpdf, swftools itself supports some fonts, but an error is reported when the PDF contains fonts that it does not support. For example, xpdf is required,XpdfIs an open-source PDF file browser. It supports Chinese characters through language packs and provides Windows and Linux versions for cross-platform deployment.

Xpdf: ftp://ftp.foolabs.com/pub/xpdf/xpdfbin-win-3.04.zip

Language Pack: ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz

Decompress xpdf to the c root directory, decompress the Language Pack to the xpdf root directory, and modify the xpdf-Chinese-Simplified file add-to-xpdfrc, add the required font directory and the specific font to be displayed, and then specify the Language Pack directory through parameters in the swftools command.

Download related files:

PDF solution Demo: http://pan.baidu.com/s/1i3mmwux

Swftools, xpdf: http://pan.baidu.com/s/1dDu1Yoh (Note: Extract in the C root directory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.