How can I create PDF using XSL and Java? - java

I believe I will have to use an XSLFO stylesheet document for the XMLs I use to convert to PDF. And then I would need to use the Transform API of Java to convert XML to PDF.

The XML data can be read using a parser and directly be converted to PDF (XML -> PDF) using a library like iText instead of using complex conversions (XML -> XSLT -> XSLFO -> PDF).

Check out Apache FOP. We use this at work and it's worked well for us. http://xmlgraphics.apache.org/fop/quickstartguide.html

Related

Creating docx/doc From Custom Xml Java Using XSLT

I have a custom XML which i have to convert into Microsoft Word Doc/Docx. There are many Java APIs like Apache POI, docx4j but i couldn't find any support for creating a word document using XSLT.
Docx4j definitely supports for conversion of docx to xml but what about the way around?
I googled and found WordML
So i thought of using XSLT to first convert my custom xml to WordML format and then later on convert WordML to doc/docx.
Custom XML----> WordML ------> Docx/Doc
My questions are :
how would i convert WordML to docx/doc (an open source method)
is it possible to achieve toc linking in WordML
Kindly guide me through if am doing it correctly or suggest a way to do it. Thanks.

Generate PDF files using iText and apache velocity template(.vm)

What is the general workflow to generate a PDF using iText and an Apache Velocity template file (.vm) in Java?
I am interested in knowing steps like: parse template file, put Java object in context and steps to be performed to generate pdf etc.
I know this is a very basic question. But I am not able to find even a single example of this type on the web. I found XDocReport, but I am interested to know other alternatives as well.
Please help me with some sample project link or at least the steps to get started.
Yes, you can.
It all depends on how complex you want the PDFs to be.
Here are the steps for basic functionality
Generate a HTML file using Apache Velocity template file (.vm).
Use com.itextpdf.text.html.simpleparser.HTMLWorker (deprecated) to parse/convert that HTML file into a PDF.
Additionally, you can use com.itextpdf.text.pdf.PdfCopy.PageStamp to add content (borders, stamps, notes, annotations etc) to an existing PDF.
There is also com.itextpdf.tool.xml.XMLWorker for more advanced HTML conversion (adding style sheets etc)
Generating PDF using iText and an Apache Velocity template file (.vm) in Java directly is not possible because:
PDF is binary format,
Velocity generates plain text content.
On other words, Velocity cannot generate PDF.
XDocReport is able to generate a docx/odt report by merging a docx/odt template which contains some Velocity/Freemarker syntax with Java context. The generated docx/odt report can be convert it to pdf/xhtml.
It works because docx/odt are a zip which contains several xml entries. If you unzip a docx you will see word/document.xml. In this entry, you will see the content that you have typed with MS Word. word/document.xml is a plain text, so Velocity can be used in this case.
Here the XDocReport process to generate pdf from a docx template which uses Velocity:
Load docx template. this step consist to unzip the docx and stores in a map each xml entries (name entry as key and byte array as value). For instance map contains a key with word/document.xml and the xml content of this entry as value.
Loop for each xml entries which must be merged with Java context. For instance word/document.xml is merged with Java context by using Velocity and the result of merge replace the word/document.xml value of the map
Rebuild a new docx by zipping each entries of the map.
At this step we have a generated docx (the report).
To convert it to another format, XDocReport provides a docx-to-pdf converter based on Apache POI and iText. Here the XDocReport process to convert a docx to pdf:
Load docx with Apache POI
Loop for each structures of POI (XWPFParagraph, etc.) to create iText structure (iText Paragraph).
Note that XDocReport is modular and you can use other converters as well.
At first,we use freemarker template to generate a html file,and then render html to a pdf file by IItextRender .Finally, we can view pdf file in browser,there has a very useful javascript tools called pdfjs. Maybe you can try it.

API's For converting A file into PDF

I want some API's and some Document So that i can convert any file into PDF..
The file may be Doc , exl, ppt ..etc .
My requirement is, i have a file EX:- Doc file and i just wants to convert it into PDF.. using java .
Any suggestion will be helpful...
I would recommend you taking a look at Flying Saucer (former xhtmlrenderer) which makes creating PDF files extremely easy from XML and HTML files (internally it uses iText).
HTML/XML can be used as a intermediate format making this a quite flexible solution.
Use
http://pdfbox.apache.org/
and
http://poi.apache.org/
If you want to generate a PDF from an XML document, you can try Apache FOP, which follows the XSL-FO standard.
http://xmlgraphics.apache.org/fop/
So a smart process could be: extract data from your various document formats using POI, odftoolkit (for OenDocument) or other tools, inject them into an XML container, and then translate them into PDF using FOP.
Apache's poi API is best for convert any file to pdf
You can use Itext . It is well documented and comes with ton of examples.

need to convert pdf to xml or html using XSLT or through java code

Hi
I want to convert pdf to xml or html using xslt or through java code..please help me..Thanks in advance..
You cannot do these conversions in XSLT because XSLT requires XML as input, and PDF is not an XML-based format.
For PDF to HTML converters in Java, see answers to this SO Question. And this article recommends PDFBox.
There are also commercial tools around for exporting or converting PDFs as HTML or XML.

How to convert xsl-fo to docx (Office Open XML) in Java?

I'm looking for an open-source or commercial friendly library in Java to convert xsl-fo to docx (Office Open XML) format.
I'm planing to use xsl-fo to produce pdf documents (with Apache FOP), so I thought generating Word documents (docx) out of the same source XML could be a good idea.
UPDATE: I forgot to mention that I'm using Java.
Alternatively, you could do: your source xml -> docx -> xsl-fo -> pdf.
or easier perhaps: source xml -> Flat OPC XML -> xsl-fo -> pdf.
Once you have a docx (or a Flat OPC XML document), transforming that to PDF via FOP is easy with docx4j (since you mention FOP, I'm assuming Java is ok for you).
The benefit of this approach is that you style your output docx as desired, and get the xsl fo "for free".
Flat OPC XML is convenient, because it is docx as a single XML file (ie no need to unzip). So you can create it easily via XSLT. To see it, create a document in Word 2007, and choose "save as .. xml".

Categories

Resources