Skip to content

Instantly share code, notes, and snippets.

@davidjgraph
Created March 27, 2014 16:25
Show Gist options
  • Save davidjgraph/9811588 to your computer and use it in GitHub Desktop.
Save davidjgraph/9811588 to your computer and use it in GitHub Desktop.
Old PDF exporter with custom font mapping
protected void writePdf(String fname, int w, int h, Color bg, String xml, HttpServletResponse response) throws DocumentException,
IOException, SAXException, ParserConfigurationException
{
response.setContentType("application/pdf");
if (fname != null)
{
response.setHeader("Content-Disposition", "attachment; filename=\"" + fname + "\"");
}
// Fixes PDF offset
w += 1;
h += 1;
Document document = new Document(new com.lowagie.text.Rectangle(w, h));
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
document.open();
PdfContentByte pcb = writer.getDirectContent();
FontMapper fontMapper = new FontMapper()
{
public BaseFont awtToPdf(Font font)
{
try
{
// FIXME written specifically for a modern mac
return BaseFont.createFont("/Library/Fonts/Arial Unicode.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
catch (DocumentException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
public Font pdfToAwt(BaseFont font, int size)
{
return null;
}
};
Graphics2D g2d = pcb.createGraphics(w, h, fontMapper);
mxGraphicsCanvas2D gc = createCanvas(g2d);
// Fixes PDF offset
gc.translate(1, 1);
renderXml(xml, gc);
gc.getGraphics().dispose();
document.close();
writer.flush();
writer.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment