/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egiz.pdfas.web.itext; import java.awt.color.ICC_Profile; import java.io.IOException; import java.io.OutputStream; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.log4j.Logger; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfArray; import com.lowagie.text.pdf.PdfDictionary; import com.lowagie.text.pdf.PdfICCBased; import com.lowagie.text.pdf.PdfName; import com.lowagie.text.pdf.PdfString; import com.lowagie.text.pdf.PdfWriter; public final class IText { private IText() { } private static final Logger LOG = Logger.getLogger(IText.class); private static final Font DEFAULT_FONT = new Font(Font.HELVETICA, 12, Font.NORMAL); private static final String DEFAULT_TTF_FONT_RESOURCE = "DejaVuSansCondensed.ttf"; private static final String DEFAULT_ICC_PROFILE_RESOURCE = "srgb.profile"; private static final Rectangle DEFAULT_PAGE_SIZE = PageSize.A4; private static final int[] DEFAULT_MARGINS_MM = { 25, 25, 25, 25 }; // top, right, bottom, left public static final String DEFAULT_FILENAME = "text"; private static Font DEFAULT_TTF_FONT; private static PdfICCBased DEFAULT_ICC_COLOR_SPACE; static { try { byte[] ttfFontData = IOUtils.toByteArray(IText.class.getResourceAsStream(DEFAULT_TTF_FONT_RESOURCE)); DEFAULT_TTF_FONT = new Font(BaseFont.createFont(DEFAULT_TTF_FONT_RESOURCE, BaseFont.WINANSI, true, true, ttfFontData, null)); } catch (Exception e) { LOG.error(e); DEFAULT_TTF_FONT = DEFAULT_FONT; } try { byte[] iccData = IOUtils.toByteArray(IText.class.getResourceAsStream(DEFAULT_ICC_PROFILE_RESOURCE)); DEFAULT_ICC_COLOR_SPACE = new PdfICCBased(ICC_Profile.getInstance(iccData)); DEFAULT_ICC_COLOR_SPACE.remove(PdfName.ALTERNATE); } catch (Exception e) { LOG.error(e); DEFAULT_ICC_COLOR_SPACE = null; } } public static byte[] createPDF(String content, boolean pdfa) throws DocumentException, IOException { ByteArrayOutputStream baOut = new ByteArrayOutputStream(); createPDF(content, pdfa, baOut); return baOut.toByteArray(); } public static byte[] createPDF(String content) throws DocumentException, IOException { return createPDF(content, false); } private static void addContent(Document document, boolean pdfa, String content) throws DocumentException { Paragraph p = new Paragraph(content, pdfa ? DEFAULT_TTF_FONT : DEFAULT_FONT); document.add(p); } public static void createPDF(String content, OutputStream outputStream) throws DocumentException, IOException { createPDF(content, false, outputStream); } public static void createPDF(String content, boolean pdfa, OutputStream outputStream) throws DocumentException, IOException { final float factor = (float) 72 / (float) 25.4; Document document = new Document( DEFAULT_PAGE_SIZE, (float) DEFAULT_MARGINS_MM[3] * factor, // left (float) DEFAULT_MARGINS_MM[1] * factor, // right (float) DEFAULT_MARGINS_MM[0] * factor, // top (float) DEFAULT_MARGINS_MM[2] * factor // bottom ); PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream); if (pdfa) { pdfWriter.setPDFXConformance(PdfWriter.PDFA1B); } document.open(); if (pdfa) { PdfDictionary pdfOutputIntent = new PdfDictionary(PdfName.OUTPUTINTENT); pdfOutputIntent.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1")); pdfOutputIntent.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1")); pdfOutputIntent.put(PdfName.S, PdfName.GTS_PDFA1); if (DEFAULT_ICC_COLOR_SPACE != null) { pdfOutputIntent.put(PdfName.DESTOUTPUTPROFILE, pdfWriter.addToBody(DEFAULT_ICC_COLOR_SPACE).getIndirectReference()); } pdfWriter.getExtraCatalog().put(PdfName.OUTPUTINTENTS, new PdfArray(pdfOutputIntent)); } addContent(document, pdfa, content); if (pdfa) { pdfWriter.createXmpMetadata(); } document.close(); } }