Troncoso
Troncoso

Reputation: 2463

Java print PDF with embedded fonts

I've written code to print a PDF using a passed in printer name and PDF url. This works, except when I pass it a PDF that has Chinese characters on it, the characters are just boxes. The Unicode font is embedded with PDF, so ideally, I'd like to utilize that somehow. Here's the code that does the printing:

PDDocument document = PDDocument.load(new URL(fileUrl));

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printer);

PageFormat pageFormat = job.defaultPage();
Paper paper = pageFormat.getPaper();
Book pBook = new Book();

List allPages = document.getDocumentCatalog().getAllPages();

for (Object pdPage : allPages)
{
    PDPage page = (PDPage) pdPage;
    double width = page.findMediaBox().getWidth();
    double height = page.findMediaBox().getHeight();
    double margin = 0.0d;
    paper.setSize(width, height);
    paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2));
    pageFormat.setOrientation(PageFormat.PORTRAIT);
    pageFormat.setPaper(paper);
    PageFormat validatePage = job.validatePage(pageFormat);
    pBook.append(document.getPrintable(allPages.indexOf(pdPage)), validatePage);
    job.setPageable(pBook);
}

job.setJobName(jobName);
job.print();

As you can see, I'm using PDFBox (version 1.8.7). I've done some searching on this, but I wasn't able to find anything useful. I did find this JIRA issue, however: https://issues.apache.org/jira/browse/PDFBOX-490

It seems maybe something was done in version 2.0, which was released this year (2016). At one point, I tried updating to the latest version, but there were so many changes, that I opted not to.

Is updating my only option? Is there anything I can do in 1.8.7?

Thanks!

Upvotes: 1

Views: 771

Answers (1)

Troncoso
Troncoso

Reputation: 2463

Per the comment by mkl, I updated to version 2.0.3 and the Chinese characters printed correctly. Here is my new code in comparison to my old to see what was necessary to update:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printer);

PageFormat pageFormat = job.defaultPage();
Paper paper = pageFormat.getPaper();
Book pBook = new Book();

PDDocument document = PDDocument.load(new URL(fileUrl).openStream()); // Needs to be a stream now

PDPageTree allPages = document.getPages(); // no getDocumentCatalog
for (Object pdPage : allPages)
{
    PDPage page = (PDPage) pdPage;
    double width = page.getMediaBox().getWidth();   // getMediaBox instead of find
    double height = page.getMediaBox().getHeight(); // getMediaBox instead of find
    double margin = 0.0d;
    paper.setSize(width, height);
    paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2));
    pageFormat.setOrientation(PageFormat.PORTRAIT);
    pageFormat.setPaper(paper);
    PageFormat validatePage = job.validatePage(pageFormat);
    pBook.append(new PDFPrintable(document), validatePage, document.getNumberOfPages()); // Completely different
}

job.setPageable(pBook); // Moved this out of the loop, just cause it only needs to be called once
job.setJobName(jobName);
job.print();

Upvotes: 1

Related Questions