Pachalum 911 Convert to PDF using JODConverter
Thanks to the work, to put it that way, I had the problem of generating a PDF of RTF, as it is for many known OpenOffice can create PDFs without using programs or external tools, I found the task a little research and found some libraries making use of OpenOffice, for more information will leave the link http://www.artofsolving.com/opensource/jodconverter
On this page we go to the downloads area and download the library JODConverter-xyz, the latest version .
As a requirement to use this library and to make the conversion you need to have OpenOffice installed, we will have it installed the following path, from the console, this is valid in GNU / Linux and Windows.
$ cd / OPEN_OFFICE_HOME / program /
Al be there type the following command:
$ soffice-headless-accept = "socket, host = localhost, port = 8100; urp;" -Nofirststartwizard
By doing so raise openoffice as a service. Within
what we are downloading from the link above, unpack the zip in any way possible, I say it this way for fans of the console on GNU / Linux.
proof Now we can perform a conversion from RTF to PDF file from the command line console by typing the following:
$ java-jar / lib / JODConverter-cli-xyzjar file.rtf file.pdf
The first parameter is the path of the file that physically exists on your hard disk and the second is the path to the PDF file to be created.
now well these libraries can be used to write some Java code. In any create an IDE project and add the libraries that are within the lib folder of the directory created when unpacking the zip you downloaded.
I've done in several ways, one, if you want to write much code and there is no extra configuration or requirement can use a process with the command line above.
Example:
String [] cmd = {"java", "-jar", "lib/jodconverter-cli-2.2.2.jar", "file.rtf", "file.pdf"}
Process p = Runtime.getRuntime (). exec (cmd);
p.waitFor ();
Or you can do as follows
OpenOfficeConnection connection = new SocketOpenOfficeConnection (8100);
connection.connect ();
converter = new OpenOfficeDocumentConverter DocumentConverter (connection); FileInput
File = new File ("file.rtf"); fileOutput
File = new File ("file.pdf");
converter.convert (FileInput, fileOutput)
connection.disconnect ();
First is sending socket connection to the port as a parameter indicated in the command line above.
then create an instance to perform the conversion.
create two instances of the java.io.File class, the first containing the RTF file and the second contains the PDF file to create.
then convert method is invoked and performs the conversion.
All this to work and shows no exception should have raised OpenOffice as a service.
It so happens that you have to make use of DocumentFormat class, as in the following example:
OpenOfficeConnection connection = new SocketOpenOfficeConnection (8100);
connection.connect ();
DocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
InputStream input = new FileInputStream("file.rtf");
DocumentFormat inputFormat = registry.getFormatByFileExtension("rtf");
OutputStream output = new FileOutputStream("file.pdf");
DocumentFormat outputFormat = registry.getFormatByFileExtension("pdf");
converter.convert(input, inputFormat, output, outputFormat);
output.close();
input.close();
connection.disconnect ();
This is an easy way to generate PDF from templates that are in RTF, DOC, and any other format that supports OpenOffice.
0 comments:
Post a Comment