use of com.lowagie.text.pdf.DefaultFontMapper in project Openfire by igniterealtime.
the class GraphServlet method writePDFContent.
private void writePDFContent(HttpServletRequest request, HttpServletResponse response, JFreeChart[] charts, Statistic[] stats, long starttime, long endtime, int width, int height) throws IOException {
try {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
writer.setPageEvent(new PDFEventListener(request));
document.open();
int index = 0;
int chapIndex = 0;
for (Statistic stat : stats) {
String serverName = XMPPServer.getInstance().getServerInfo().getXMPPDomain();
String dateName = JiveGlobals.formatDate(new Date(starttime)) + " - " + JiveGlobals.formatDate(new Date(endtime));
Paragraph paragraph = new Paragraph(serverName, FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD));
document.add(paragraph);
paragraph = new Paragraph(dateName, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.PLAIN));
document.add(paragraph);
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
Paragraph chapterTitle = new Paragraph(++chapIndex + ". " + stat.getName(), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD));
document.add(chapterTitle);
// total hack: no idea what tags people are going to use in the description
// possibly recommend that we only use a <p> tag?
String[] paragraphs = stat.getDescription().split("<p>");
for (String s : paragraphs) {
Paragraph p = new Paragraph(s);
document.add(p);
}
document.add(Chunk.NEWLINE);
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphs2D = template.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
charts[index++].draw(graphs2D, rectangle2D);
graphs2D.dispose();
float x = (document.getPageSize().width() / 2) - (width / 2);
contentByte.addTemplate(template, x, writer.getVerticalPosition(true) - height);
document.newPage();
}
document.close();
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength is needed for MSIE!!!
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
} catch (DocumentException e) {
Log.error("error creating PDF document: " + e.getMessage());
}
}
use of com.lowagie.text.pdf.DefaultFontMapper in project adempiere by adempiere.
the class Document method writePDF.
private static void writePDF(Pageable pageable, OutputStream output) {
try {
final PageFormat pf = pageable.getPageFormat(0);
final com.lowagie.text.Document document = new com.lowagie.text.Document(new Rectangle((int) pf.getWidth(), (int) pf.getHeight()));
final PdfWriter writer = PdfWriter.getInstance(document, output);
writer.setPdfVersion(PdfWriter.VERSION_1_2);
document.open();
final DefaultFontMapper mapper = new DefaultFontMapper();
//Elaine 2009/02/17 - load additional font from directory set in PDF_FONT_DIR of System Configurator
String pdfFontDir = MSysConfig.getValue(PDF_FONT_DIR, "");
if (pdfFontDir != null && pdfFontDir.trim().length() > 0) {
pdfFontDir = pdfFontDir.trim();
File dir = new File(pdfFontDir);
if (dir.exists() && dir.isDirectory())
mapper.insertDirectory(pdfFontDir);
}
//
final float w = (float) pf.getWidth();
final float h = (float) pf.getHeight();
final PdfContentByte cb = writer.getDirectContent();
for (int page = 0; page < pageable.getNumberOfPages(); page++) {
if (page != 0) {
document.newPage();
}
final PdfTemplate tp = cb.createTemplate(w, h);
final Graphics2D g2 = tp.createGraphics(w, h, mapper);
tp.setWidth(w);
tp.setHeight(h);
pageable.getPrintable(page).print(g2, pf, page);
g2.dispose();
cb.addTemplate(tp, 0, 0);
writer.releaseTemplate(tp);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.lowagie.text.pdf.DefaultFontMapper in project beast-mcmc by beast-dev.
the class MapperFrameOld method doExportPDF.
public final void doExportPDF() {
FileDialog dialog = new FileDialog(this, "Export PDF Image...", FileDialog.SAVE);
dialog.setVisible(true);
if (dialog.getFile() != null) {
File file = new File(dialog.getDirectory(), dialog.getFile());
Rectangle2D bounds = mapperPanel.getExportableComponent().getBounds();
Document document = new Document(new com.lowagie.text.Rectangle((float) bounds.getWidth(), (float) bounds.getHeight()));
try {
// step 2
PdfWriter writer;
writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate((float) bounds.getWidth(), (float) bounds.getHeight());
Graphics2D g2d = tp.createGraphics((float) bounds.getWidth(), (float) bounds.getHeight(), new DefaultFontMapper());
mapperPanel.getExportableComponent().print(g2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);
} catch (DocumentException de) {
JOptionPane.showMessageDialog(this, "Error writing PDF file: " + de, "Export PDF Error", JOptionPane.ERROR_MESSAGE);
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, "Error writing PDF file: " + e, "Export PDF Error", JOptionPane.ERROR_MESSAGE);
}
document.close();
}
}
Aggregations