Java doc to PDF

Source: Internet
Author: User

The mainstream WORD to PDF tool in the market is two: OpenOffice and Microsoft Office go
Plug-ins, you can implement WORD to PDF functionality by deploying these two tools.

1:

Microsoft provides a conversion plug-in that implements the Office to PDF feature, which
Saveaspdfandxps. This plug-in is a COM component, and for languages such as C + +, C #, you can directly make
, in the case of the JAVA language, you need to invoke the COM component through Jacob.
Saveaspdfandxps plug-in requires a Windows server as a conversion server.
Install the version above Microsoft Office2007 before installing the Saveaspdfandxps plug-in.
Finally, the COM component is called to implement the transformation.
Official address: https://msdn.microsoft.com/en-us/library/dd301166 (v=nav.90). aspx

2.

OpenOffice is an open-source office suite with MS Word,excel,powerpoint and more.
Software that supports multiple formats, including MS Office 2007, and enables
Out as PDF files.
This scenario is to install OpenOffice on a Linux server and then convert by OpenOffice command
Pdf.
Official website: http://www.openoffice.org/

Under Ubuntu:

    1. Tar-xvzf apache_openoffice_4.1.3_linux_x86-64_install-deb_zh-cn.tar.gz
    2. CD zh-cn/debs/
    3. sudo dpkg-i *.deb
    4. CD desktop-integration/
    5. sudo dpkg-i openoffice4.1-debian-menus_4.1.3-9783_all.deb

Soffice--headless--accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"--nofirststartwizard &

Start the service, # Netstat-an|more, to see if the boot was successful (if there is a 8100 port service)

 Packageopenofficetest; ImportJava.io.File; ImportJava.io.FileInputStream; ImportJava.io.FileOutputStream; ImportJava.io.InputStream; ImportJava.io.OutputStream; Importjava.net.ConnectException; ImportCom.artofsolving.jodconverter.DefaultDocumentFormatRegistry; ImportCom.artofsolving.jodconverter.DocumentConverter; ImportCom.artofsolving.jodconverter.DocumentFormat; Importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; Importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; ImportCom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  Public classWord2pdf { Public Static intPORT = 8100;  Public Static voidMain (string[] args) {String path1= "/tmp/1.doc"; String path2= "/tmp/2.pdf"; Try{wordtopdf (path1, path2); } Catch(Exception e) {e.printstacktrace (); }      }              Public Static voidwordtopdf (String path1, string path2)throwsException {File file1=NewFile (path1); File file2=NewFile (path2); //Get file FormatDefaultdocumentformatregistry Formatreg =NewDefaultdocumentformatregistry (); Documentformat Pdfformat= Formatreg.getformatbyfileextension ("PDF"); Documentformat Docformat= Formatreg.getformatbyfileextension ("Doc"); //Stream form of streamInputStream InputStream =NewFileInputStream (FILE1); OutputStream OutputStream=NewFileOutputStream (file2); /**          *           */openofficeconnection Connection=NewSocketopenofficeconnection (PORT);          System.out.println (connection); Try{connection.connect (); Documentconverter Converter=NewOpenofficedocumentconverter (connection); Converter.convert (InputStream, Docformat, OutputStream, Pdfformat); } Catch(connectexception e) {e.printstacktrace (); } finally {              if(Connection! =NULL) {connection.disconnect (); Connection=NULL; }          }      }       }  

However, after testing, OpenOffice conversion speed is obviously very slow, mainly in the acquisition of openofficeconnection this piece, I have not yet found a way to significantly improve the speed, there is a third method based on LibreOffice to do the conversion.

Prerequisites: To install LibreOffice, libreoffice-headless

installation command:

Yum Install Libreoffice-y

Yum Install Libreoffice-headless-y

Conversion command: LibreOffice--headless--convert-to pdf:writer_pdf_export--outdir/tmp//tmp/test.doc

Where/tmp/test.doc is the doc file for the test, the resulting PDF file will be under/tmp/and the name will be the same as the doc name.

The following is a way to return the PDF file stream as input to the doc file stream in the project.

 Public Static byte[] Topdf (byte[] b, String sourceFileName)throwsexception{File tempdir=NULL; Try{tempdir=Files.createtempdir (); String Canonicalpath=Tempdir.getcanonicalpath (); File File=NewFile (Canonicalpath + "/" +sourceFileName); OutputStream OS=Newfileoutputstream (file); Bufferedoutputstream Bufferedoutput=Newbufferedoutputstream (OS);                        Bufferedoutput.write (b); String Command= "LibreOffice"; Process proc=NewProcessbuilder (Command, "--headless", "--convert-to", "Pdf:writer_pdf_export", "--outdir", Canonicalpath, Canonicalpath + "/" +sourcefilename). Redirecterrorstream (true). Start (); ArrayList<String> output =NewArraylist<string>(); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Proc.getinputstream ())); String Line=NULL;  while(line = Br.readline ())! =NULL) Output.add (line); Logger.info ("Output of the Execute PDF conversion command:" +stringutils.join (output, System.lineseparator ())); if(0! =proc.waitfor ())Throw NewException ("Conversion failed"); file[] Files=Tempdir.listfiles ();  for(File file2:files) {if(File2.getpath (). EndsWith (". pdf")) {                    returnIoutils.tobytearray (NewFileInputStream (file2)); }            }            return NULL; }finally{            if(TempDir! =NULL) fileutils.deletedirectory (tempdir); }    }

Java doc to PDF

Related Article

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.