Exporting .jasper to .pdf with Java

Hi all!

New to Jaspersoft and Java, but I understand the basics. As the titles sugested, I want to export my .jasper while to .pdf, but i keep getting error messages and my pdf file won't open.

Does anyone know, why the pdf export is not succesfull? Let me know if you need more information!

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.ExporterInput;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.view.JasperViewer;
 
public class JasperReportHelloWorld {
 
    public static void main(String[] args) {
 
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
 
            String dbURL;
            String username;
            String password;
            if (args.length != 0) {
                dbURL = "jdbc:mysql://" + args[0] + ":" + args[1] + "/" + args[2];
                username = args[3];
                password = args[4];
            } else {
                dbURL = "jdbc:mysql://127.0.0.1:-/-";
                username = "-";
                password = "-";
            }
            System.out.println(dbURL);
            Connection conn = DriverManager.getConnection(dbURL, username, password);
 
            String reportSrcFile = "C:\\Users\\-\\-\\-\\.-\\.JRS_project\\" + args[5];
            String reportCompiledFile = JasperCompileManager.compileReportToFile(reportSrcFile);
 
            Map<String, Object> parameters = new HashMap<String, Object>();
 
            JasperPrint print = JasperFillManager.fillReport(reportCompiledFile, parameters, conn);
 
            JasperViewer.viewReport(print, true);
 
            // String pdfFileName = args[6];
            // JasperExportManager.exportReportToPdfFile(print, pdfFileName);
 
            // File pdf = File.createTempFile("output.", ".pdf");
            // JasperExportManager.exportReportToPdfStream(print, new FileOutputStream(pdf));
 
            File pdf = File.createTempFile("output.", ".pdf");
            FileOutputStream outputStream = new FileOutputStream(pdf);
 
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setExporterInput((ExporterInput) new SimpleExporterInput(print));
            // File pdf = File.createTempFile("output.", ".pdf");
            // FileOutputStream outputStream = new FileOutputStream(pdf);
            exporter.setExporterOutput((OutputStreamExporterOutput) new SimpleOutputStreamExporterOutput(outputStream));
            SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
            configuration.setMetadataAuthor("Willem");
            exporter.setConfiguration(configuration);
            exporter.exportReport();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Greetings, w.

wim040's picture
31
Joined: May 24 2023 - 1:04pm
Last seen: 4 months 1 day ago

Thank you for posting to the Jaspersoft Community. Our team of experts has read your question and we are working to get you an answer as quickly as we can. If you have a Jaspersoft Professional Subscription plan, please visit https://support.tibco.com/s/ for direct access to our technical support teams offering guaranteed response times.
 

mrajkuma - 4 months 3 days ago

2 Answers:

Try to do outputStream.close(); after exporter.exportReport();

Regards,

Lucian

lucianc's picture
87569
Joined: Jul 17 2006 - 1:10am
Last seen: 19 hours 10 min ago

@lucianc Thanks for the response! But sadly it does not work, I keep getting the following error messages:
jdbc:mysql://127.0.0.1:3306/willem_bi
Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/pdf/FontMapper
        at net.sf.jasperreports.export.pdf.classic.ClassicPdfProducerFactory.createProducer(ClassicPdfProducerFactory.java:44)
        at net.sf.jasperreports.engine.export.JRPdfExporter.createPdfProducer(JRPdfExporter.java:902)
        at net.sf.jasperreports.engine.export.JRPdfExporter.initExport(JRPdfExporter.java:733)
        at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:694)
        at JasperReportHelloWorld.main(JasperReportHelloWorld.java:67)
Caused by: java.lang.ClassNotFoundException: com.lowagie.text.pdf.FontMapper
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        ... 5 more

Greeings, Willem

wim040's picture
31
Joined: May 24 2023 - 1:04pm
Last seen: 4 months 1 day ago
Feedback
randomness