Created
March 27, 2014 16:25
-
-
Save davidjgraph/9811588 to your computer and use it in GitHub Desktop.
Old PDF exporter with custom font mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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