How to create a PDF report from a LaTeX template file : to PDF « PDF « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » PDF » to PDF 
29.77.3.How to create a PDF report from a LaTeX template file
/**
 * How to create a PDF report from a LaTeX template file?
 
 * This sample shows how to create a very simple invoice from an invoice template.
 
 * Requirements:
 * - The free Java LaTeX Report (JLR) library - http://nixo-soft.de/en/category/Downloads/page/libs/JavaLatexReport.php
 * - Java Runtime Environment (JRE) 6
 * - Installed or portable LaTeX Distribution(e.g. MiKTeX or MiKTeX Portable)
 
 * For a more detailed Tutorial on how creating a LaTeX template file or how to use Java LaTeX Report,
 * look at http://www.nixo-soft.de/tutorials/jlr/JLRTutorial.html
 */

import de.nixosoft.jlr.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class Main {

    public static void main(String[] args) {

        File workingDirectory = new File("C:" + File.separator + "Invoices");

        File template = new File(workingDirectory.getAbsolutePath() + File.separator + "invoiceTemplate.tex");

        File tempDir = new File(workingDirectory.getAbsolutePath() + File.separator + "temp");
        if (!tempDir.isDirectory()) {
            tempDir.mkdir();
        }

        File invoice1 = new File(tempDir.getAbsolutePath() + File.separator + "invoice1.tex");
        File invoice2 = new File(tempDir.getAbsolutePath() + File.separator + "invoice2.tex");



        try {

            HashMap<String, String> data = new HashMap<String, String>();

            data.put("Number""1");
            data.put("Customer name""Ivan Pfeiffer");
            data.put("Customer street""Schwarzer Weg 4");
            data.put("Customer zip""13505 Berlin");
            data.put("Development""Software");
            data.put("Price""500");

            JLRConverter converter = new JLRConverter("::"":::");
            if (!converter.parse(template, invoice1, data)) {
                System.out.println(converter.getErrorMessage());
            }

            data.put("Number""2");
            data.put("Customer name""Mike Mueller");
            data.put("Customer street""Prenzlauer Berg 12");
            data.put("Customer zip""10405 Berlin");
            data.put("Development""Hardware");
            data.put("Price""2350");

            if (!converter.parse(template, invoice2, data)) {
                System.out.println(converter.getErrorMessage());
            }


            File desktop = new File(System.getProperty("user.home"+ File.separator + "Desktop");

            JLRGenerator pdfGen = new JLRGenerator();

            pdfGen.deleteTempTexFile(false);

            if (!pdfGen.generate(invoice1, desktop, workingDirectory)) { 
                System.out.println(pdfGen.getErrorMessage());
            }

            JLROpener.open(pdfGen.getPDF());

            if (!pdfGen.generate(invoice2, desktop, workingDirectory)) {
                System.out.println(pdfGen.getErrorMessage());
            }

            JLROpener.open(pdfGen.getPDF());

        catch (IOException ex) {
            System.err.println(ex.getMessage());
        }
    }
}
29.77.to PDF
29.77.1.Create Pdf document from JTable
29.77.2.JTextPane To Pdf
29.77.3.How to create a PDF report from a LaTeX template file
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.