You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Jeffrey Begley <je...@librarybound.com> on 2016/04/11 19:35:05 UTC
PDFMergerUtility in PdfBox 2.0.0 fails to embed type1 font
Hello PdfBox Team,
I ran into a problem where PDFs programmatically created and merged using PdfBox fail to display all characters written in a type1 font. When opening such a file with Adobe it shows the error message "Cannot find or create the font 'NotoSans'. Some characters may not display or print correctly" and every character displays as a black dot.
I believe that org.apache.pdfbox.multipdf.PDFMergerUtility has a bug where it is failing to subset type1 fonts for appended documents unless individual documents are saved first.
To reproduce the issue, I've included a minimal example below. The example is based on one of the PdfBox examples except it loads a font from the filesystem and "merges" the single PDF. If you run the code as-is, you'll get a PDF with the missing font. If you run it again after uncommenting the line "document.save(new ByteArrayOutputStream());" then the PDF contains the font and works properly.
Thanks,
Jeffrey
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
// Create a new font object selecting one of the PDF base fonts
// use a different font
// PDFont font = PDType1Font.HELVETICA_BOLD;
PDFont font = PDType0Font.load(document, new File("NotoSans/NotoSans-Regular.ttf"));
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString("Hello World");
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
// Uncomment the next line to make it work. Presumably because it subsets the fonts.
// document.save(new ByteArrayOutputStream());
PDFMergerUtility merger = new PDFMergerUtility();
// Merge one document
PDDocument outDoc = new PDDocument();
for (PDDocument curDoc : Arrays.asList(document))
{
merger.appendDocument(outDoc, curDoc);
curDoc.close();
}
outDoc.save("Merged.pdf");
outDoc.close();
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
Re: PDFMergerUtility in PdfBox 2.0.0 fails to embed font
Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,
I haven't tested your code, because I believe it is a known problem:
https://issues.apache.org/jira/browse/PDFBOX-3243
see workaround there. Btw the title is incorrect, you're using a TT
font, not a Type1 font.
Tilman
Am 11.04.2016 um 19:35 schrieb Jeffrey Begley:
> Hello PdfBox Team,
> I ran into a problem where PDFs programmatically created and merged using PdfBox fail to display all characters written in a type1 font. When opening such a file with Adobe it shows the error message "Cannot find or create the font 'NotoSans'. Some characters may not display or print correctly" and every character displays as a black dot.
>
> I believe that org.apache.pdfbox.multipdf.PDFMergerUtility has a bug where it is failing to subset type1 fonts for appended documents unless individual documents are saved first.
>
> To reproduce the issue, I've included a minimal example below. The example is based on one of the PdfBox examples except it loads a font from the filesystem and "merges" the single PDF. If you run the code as-is, you'll get a PDF with the missing font. If you run it again after uncommenting the line "document.save(new ByteArrayOutputStream());" then the PDF contains the font and works properly.
>
> Thanks,
>
> Jeffrey
>
> // Create a document and add a page to it
> PDDocument document = new PDDocument();
> PDPage page = new PDPage();
> document.addPage(page);
>
> // Create a new font object selecting one of the PDF base fonts
> // use a different font
> // PDFont font = PDType1Font.HELVETICA_BOLD;
> PDFont font = PDType0Font.load(document, new File("NotoSans/NotoSans-Regular.ttf"));
>
> // Start a new content stream which will "hold" the to be created content
> PDPageContentStream contentStream = new PDPageContentStream(document, page);
>
> // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
> contentStream.beginText();
> contentStream.setFont(font, 12);
> contentStream.moveTextPositionByAmount(100, 700);
> contentStream.drawString("Hello World");
> contentStream.endText();
>
> // Make sure that the content stream is closed:
> contentStream.close();
>
> // Save the results and ensure that the document is properly closed:
> // Uncomment the next line to make it work. Presumably because it subsets the fonts.
> // document.save(new ByteArrayOutputStream());
>
> PDFMergerUtility merger = new PDFMergerUtility();
>
> // Merge one document
> PDDocument outDoc = new PDDocument();
> for (PDDocument curDoc : Arrays.asList(document))
> {
> merger.appendDocument(outDoc, curDoc);
> curDoc.close();
> }
>
> outDoc.save("Merged.pdf");
> outDoc.close();
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org