Writing web service to convert documents to PDF
Is it possible to write a RESTful web servi开发者_运维技巧ce that will accept files from a client, convert those files to PDF files and then will send the result back to the client?
Any information on the topic would be helpful.
x-to-PDF conversion and PDF generation:
- Apache FOP - XSL-FO to PDF
- iText - low-level PDF API
- Flyingsaucer - XHTML to PDF
REST:
- JAX-RS - the Java API for REST.
Some years ago i've made a simple but poerfull class to convert HTML to PDF. Really usefull:
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
/**
* @Autor Eder Baum
*/
public class Html2Pdf {
public static void convert(String input, OutputStream out) throws DocumentException{
convert(new ByteArrayInputStream(input.getBytes()), out);
}
public static void convert(InputStream input, OutputStream out) throws DocumentException{
Tidy tidy = new Tidy();
Document doc = tidy.parseDOM(input, null);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(out);
}
}
Usage:
OutputStream os = new FileOutputStream("C:\\hello.pdf");;
Html2Pdf.convert("<h1 style=\"color:red\">Hello PDF</h1>", os);
os.close();
All Files here: https://dl.getdropbox.com/u/15403/Html2PDF.zip
I see from your own comment that you are interested in converting Office files to PDF from Java.
A shameless product plug perhaps, as I have worked on this product myself, but check out this web service for converting Common document formats to PDF. Java sample code is included in the post.
You might want to supply more information about the types of files you are hoping to convert to PDF since that will determine the underlying technolgy for the conversion process. Apart from that you have only a document transport system to implement in your web service which is independent of the conversion process. Docmosis can be embedded server side which can provide all the document conversion filters available in OpenOffice, but also allows you to populate and manipulate documents.
I try some libs for .xls* to .pdf converting:
aspose-cells - good result, need for pay
spire.xls.free - sometime give incorrect .pdf, is free
itextpdf - worst, is free
libreoffice - use libreoffice for converting, need windows server - is free
https://github.com/fedor83/xlsToPdfConverter
精彩评论