Jump to content
Changes to the Jaspersoft community edition download ×

Problem with PdfXConformanceException. Font related.


phnxck

Recommended Posts

Hi All,

I am trying to produce PDF file that satisfies PDF/A1a conformance. My main goal is to somehow embed extractable document structure inside it (also known as being 'tagged'/'Tagged PDF'), but I run in to a problem much earlier. When I try do generate the report, I get the following exception:

 

Exception in thread "main" com.lowagie.text.pdf.PdfXConformanceException: All the fonts must be embedded. This one isn't: Helvetica

at com.lowagie.text.pdf.internal.PdfXConformanceImp.checkPDFXConformance(Unknown Source)

at com.lowagie.text.pdf.PdfWriter.addSimple(Unknown Source)

at com.lowagie.text.pdf.PdfContentByte.setFontAndSize(Unknown Source)

at com.lowagie.text.pdf.PdfDocument.writeLineToContent(Unknown Source)

at com.lowagie.text.pdf.ColumnText.go(Unknown Source)

at com.lowagie.text.pdf.ColumnText.go(Unknown Source)

at net.sf.jasperreports.engine.export.JRPdfExporter.writePageAnchor(JRPdfExporter.java:708)

at net.sf.jasperreports.engine.export.JRPdfExporter.exportReportToStream(JRPdfExporter.java:632)

at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:362)

at com.bms.goro.test.jasper.ReportGeneratorApp.generateReport(ReportGeneratorApp.java:53)

at com.bms.goro.test.jasper.ReportGeneratorApp.main(ReportGeneratorApp.java:32)

 

I do not use Helvetica font, so I am not trying to embed it anyhow. Moreover I have read that this font must not be embedded as it is one of Base14 fonts which are built in as default in pdf viewers.

 

The only reasonable topic about PDF/A I found is:

http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=92542

but still after reading it I am not able to resolve my problem. I am not sure if it is a matter of some configuration in code with exporter or maybe some configuration in iReport.

 

Below there is code that generates the report and simple jrxml temaplate code that I use (after compiling through iReport).

If it matters, I use iReport and JasperReports library both in 4.5.1 version. I also attach compressed full sources of my test (maven) project.

Code:

 



Post Edited by phnxck at 05/07/2012 14:44
Link to comment
Share on other sites

  • 2 months later...
  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

After some time I managed to solve this problem. The solution is to declare default style which will be assigned implicitly to all text elements. If you would like to make some of the components look different (i.e. bold, italic, other color, etc.) then you have to declare another styles and assign them explicitly.

If you would like an example, check this page: http://jasperforge.org/plugins/mantis/view.php?id=2819

There you can find 'xlsdatasource.zip' attachment, which contains different report output files examples (pdf, dox, xls, etc). There are two pdfs, and the one (at xlsdatasource/build/reports/) with name "XlsDataSourceReport2.pdf" was generated by pdf2() method from "XlsDataSourceApp.java" (xlsdatasource/src/) which creates A1A compliant pdfs. So there you can find implementation for the java code. And since it works correctly than you can look at being used template (.jrxml) so that you can see how it has to be build using styles.

I hope it is clear, if you have any other questions, just ask.



Post Edited by phnxck at 07/13/2012 09:07
Link to comment
Share on other sites

Dear phnxck,

Thx a lot for your quick reply!! I went through the topics you indicated and was able - Hoera Hoera!! - to generate the PDF/A1-B file. Magic! /tools/fckeditor/editor/images/smiley/msn/lightbulb.gif

Very strange that you need to define a default style before you can get this working though... but no mater how you do it, the result is there: It works like a charm!

Thanks again! Highly appreciated!

Maarten

Link to comment
Share on other sites

It was odd also to me (this thing with style). In fact you do not need to create default style, but this way you would have to remember to assign explicitly some style to all elements. Anyway I am glad I could help, cause I can remember how horror it was for me :)

 

Peter.

Link to comment
Share on other sites

  • 4 months later...

Dear phnxck,

I can't load the page with your example : it's not available any more : http://jasperforge.org/plugins/mantis/view.php?id=2819.

I would like to know how to define a default font style which will be assigned implicitly to all text elements but i don't know how.

I don't use JasperReport, only itext v 5.3.3 with the class PdfAWriter and elements like PdfPTable, PdfPCell, Paragraph, Phrase... I know how to embed font to insert paragraph or phrase but i didn't manage to define a default when i work with a PdfPtable : i have this type of error : "All the fonts must be embedded. This one isn't: Helvetica".

Here is my code, simplified to facilitate the analysis :


public class MainClass
{
    public static Font BOLD;
    public static Font NORMAL;
    protected static BaseFont timesbd;

    public static void main(String[] args) throws IOException, DocumentException
    {
        MainClass example = new MainClass();
        example.createPdfA("c:/test/test.pdf");
    }
    
    public void createPdfA(String filename) throws IOException, DocumentException {
        
        FontFactory.register("c:/NewYorker.ttf");
        try
        {
            Font fontTest = FontFactory.getFont("new yorker","UTF-8",BaseFont.EMBEDDED);
            timesbd = fontTest.getBaseFont();
            BOLD = new Font(timesbd, 12);
            NORMAL = new Font(times, 12);
            
        }catch(Exception e)
        {
            e.printStackTrace();
        }
       
        File dir = new File("c:/test");
        if (dir.exists() != true)
        {
            dir.mkdir();
        }

        try {
            Path path = Paths.get("c:/test/test.pdf");
            if (path != null) Files.createDirectories(path.getParent());
        }catch (IOException e) {
            e.printStackTrace();
        }
        

        Document document = new Document();

        PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1B);

        document.open();

        float[] columnWidths = { 3.5f, 4.5f, 2.8f };
        PdfPTable table = new PdfPTable(columnWidths);
        
        PdfPCell defaultCell = table.getDefaultCell();
        defaultCell.setPhrase(new Phrase("",NORMAL));
        
        table.setWidthPercentage(90);
        table.setHorizontalAlignment(Element.ALIGN_RIGHT);
        
        PdfPCell cell1 = new PdfPCell(new Phrase("test 1",BOLD));
        cell1.setBorder(0);
        cell1.setIndent(27);
        cell1.setLeading(4.5f, 1);
        
        PdfPCell cell2 = new PdfPCell(new Phrase("test 2",NORMAL));
        cell2.setBorder(0);
        cell2.setIndent(27);
        cell2.setLeading(4.5f, 1);
        
        PdfPCell cell3 = new PdfPCell(new Phrase("test 2",FONT_NORMAL));
        cell3.setBorder(0);
        cell3.setIndent(27);
        cell3.setLeading(4.5f, 1);
        
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        
        List chunks = table.getChunks();
        for(Chunk chunk : chunks)
        {
            if(!chunk.getFont().getBaseFont().isEmbedded())
            {
                chunk.setFont(NORMAL);
            }
        }
        
        document.add(table);
        document.close();
    }

}

Link to comment
Share on other sites

  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...