Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Printable to PDF
    text
    copied!<p>I am working on a user story that asks to be able to print all the existing print out as PDFs too. To do this I am rendering the <code>java.awt.Printable</code> to a <code>BufferedImage</code> and putting this image into a PDF using Apache's PDFBox but I am having a couple of issues using this method:</p> <ul> <li>If I use <code>PDJPeg</code> the image becomes blurry.</li> <li>If I use <code>PDPixelMap</code> the resulting PDF is completely black.</li> </ul> <p>The only other way I can think of is to make a interface, i.e. <code>Printer</code>, and then making two subclasses i.e. <code>PhysicalPrinter</code> and <code>PDFPrinter</code> but this would require that I reimplement all existing <code>java.awt.Printable</code>s to use this new interface.</p> <p>Here is a SSCCE of my current print to PDF method:</p> <pre><code>import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.io.File; import java.util.Random; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg; public class Printer { private Printer() {} public static boolean printToPDF(String file, Printable printable) { try { PageFormat pageFormat = new PageFormat(); PDDocument doc = new PDDocument(); int width = (int)(pageFormat.getWidth()); int height = (int)(pageFormat.getHeight()); int currentPage = 0; while(true) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = image.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, width, height); g2d.setColor(Color.BLACK); g2d.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12)); if(printable.print(g2d, pageFormat, currentPage) == Printable.NO_SUCH_PAGE) { break; } ImageIO.write(image, "png", new File("test.png")); PDPage page = new PDPage(); doc.addPage(page); PDJpeg img = new PDJpeg(doc, image); //Blurry //PDPixelMap img = new PDPixelMap(doc, image); //Black PDPageContentStream content = new PDPageContentStream(doc, page); content.drawImage(img, 0, 0); content.close(); ++currentPage; } doc.save(file); doc.close(); } catch(Exception e) { e.printStackTrace(); } return true; } public static void main(String[] args) { Printable printable = new Printable() { @Override public int print(Graphics g, PageFormat pf, int index) throws PrinterException { if(index &gt; 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); Random rand = new Random(); for(int i = 0; i &lt; 30; ++i) { int x = rand.nextInt((int)pf.getImageableWidth()); int y = rand.nextInt((int)pf.getImageableHeight()); g2d.drawString("Test", x, y); } return PAGE_EXISTS; } }; printToPDF("Test.pdf", printable); } } </code></pre> <hr> <p><strong>Edit</strong></p> <p>I managed to solved the black <code>PDPixelMap</code>, it was down to the <code>BUfferedImage</code> using the wrong image type. I changed it from <code>BufferedImage.TYPE_INT_RGB</code> to <code>BufferedImage.TYPE_4BYTE_ABGR</code>. Unfortunately the resultant PDF is still blurry.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload