Example usage for com.itextpdf.text.pdf PdfWriter getInstance

List of usage examples for com.itextpdf.text.pdf PdfWriter getInstance

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfWriter getInstance.

Prototype


public static PdfWriter getInstance(final Document document, final OutputStream os) throws DocumentException 

Source Link

Document

Use this method to get an instance of the PdfWriter.

Usage

From source file:com.vimbox.hr.LicensePDFGenerator.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  ww.j  a  v a  2s.  com*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/pdf");
    String fileName = request.getParameter("license_name");
    String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
    String path = System.getProperty("user.dir") + "/documents/licenses/" + fileName;
    path = path.replaceAll("%20", " ");
    if (ext.equalsIgnoreCase("pdf")) {
        FileInputStream baos = new FileInputStream(path);

        OutputStream os = response.getOutputStream();

        byte buffer[] = new byte[8192];
        int bytesRead;

        while ((bytesRead = baos.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }

        os.flush();
        os.close();
    } else {
        try {
            // Document Settings //
            Document document = new Document();
            PdfWriter.getInstance(document, response.getOutputStream());
            document.open();

            // Loading MC //
            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            // the cell object
            PdfPCell cell;

            Image img = Image.getInstance(path);
            int indentation = 0;
            float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin()
                    - indentation) / img.getWidth()) * 100;
            img.scalePercent(scaler);
            //img.scaleAbsolute(80f, 80f);
            cell = new PdfPCell(img);
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
            document.add(table);
            //-----------------------------------//
            document.close();
        } catch (DocumentException de) {
            throw new IOException(de.getMessage());
        }
    }

}

From source file:com.wabacus.system.component.application.report.abstractreport.AbsReportType.java

License:Open Source License

public ByteArrayOutputStream displayOnPdf() {
    if (!rrequest.checkPermission(rbean.getId(), null, null, Consts.PERMISSION_TYPE_DISPLAY))
        return null;
    if (rrequest.checkPermission(rbean.getId(), Consts.BUTTON_PART, "type{" + Consts.DATAEXPORT_PDF + "}",
            Consts.PERMISSION_TYPE_DISABLED))
        return null;
    if (rrequest.isPdfPrintAction()) {
        pdfbean = rbean.getPdfPrintBean();
    } else if (rbean.getDataExportsBean() != null) {//PDF?PDF<dataexport/>
        pdfbean = (PDFExportBean) this.currentDataExportBean;
    }//from w w  w.j  ava 2s . c om
    document = new Document();
    if (pdfbean != null) {//?pdf<dataexport/>
        document.setPageSize(pdfbean.getPdfpagesizeObj());//PDF
        pdfpagesize = pdfbean.getPagesize();
        pdfwidth = pdfbean.getWidth();
        isFullpagesplit = pdfbean.isFullpagesplit();
    } else {//?pdf<dataexport/>?
        pdfpagesize = rbean.getLstPagesize().get(0);
        isFullpagesplit = true;
    }
    if (pdfwidth <= 10f)
        pdfwidth = 535f;
    try {
        ByteArrayOutputStream baosResult = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baosResult);
        document.open();
        boolean flag = true;
        if (pdfbean != null && pdfbean.getInterceptorObj() != null) {//?
            flag = pdfbean.getInterceptorObj().beforeDisplayReportWithoutTemplate(document, this);
        }
        if (flag) {
            showReportOnPdfWithoutTpl();
            if (pdfDataTable != null) {
                document.add(pdfDataTable);
                if (pdfbean != null && pdfbean.getInterceptorObj() != null) {
                    pdfbean.getInterceptorObj().afterDisplayPdfPageWithoutTemplate(document, this);
                }
            }
        }
        if (pdfbean != null && pdfbean.getInterceptorObj() != null) {
            pdfbean.getInterceptorObj().afterDisplayReportWithoutTemplate(document, this);
        }
        document.close();
        rrequest.releaseDBResources();
        return baosResult;
    } catch (Exception e) {
        throw new WabacusRuntimeException("" + rbean.getPath() + "PDF", e);
    }
}

From source file:com.warehouse.abstractController.OrderMenuAbstractController.java

public void handleGeneratePDF() {
    try {//from  w ww .  j a  va  2 s.  c  o  m
        OutputStream file = new FileOutputStream(new File("./Test.pdf"));
        Document document = new Document();
        PdfWriter.getInstance(document, file);
        document.open();

        document.add(new Paragraph("Order information"));

        for (Order o : orderDao.getOrderList())
            if (o.getId() == Integer.parseInt(Cookie.getInstance().get("orderID")))
                document.add(new Paragraph("Order (" + o.getId() + ") " + o.getItems() + " " + o.getDate() + " "
                        + o.getClient().getPhone()));

        document.add(new Paragraph(new Date().toString()));
        document.close();

        file.close();

        AlertBox.getInstance().display(getClass().getSimpleName(), "Successful generated *.PDF file !");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.wesley.creche.services.pdf.createHrPdf.java

public void writePdfHrReport() throws DocumentException, IOException, SQLException {

    createFolderIfNotExist();//  ww  w . jav a 2  s  .c o m

    Date date = new Date();
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    String today = df.format(date);
    String fileName = "C:\\creche\\reports\\HRReport" + today + "_.pdf";

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));

    document.open();

    addTitle(document);
    addLine(document);
    addEmptyLine(document, 2);

    try {
        document.add(new Paragraph(getEmpName()));
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(createHrPdf.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        document.add(new Paragraph(getEmpLastName()));
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(createHrPdf.class.getName()).log(Level.SEVERE, null, ex);
    }

    document.close();
}

From source file:com.wesley.creche.services.pdf.createPdf.java

public void writePdfFinancialReport(String name, String startDate, String accStatus, String amountDue)
        throws DocumentException, IOException {

    createFolderIfNotExist();/*from   w  ww. j a  v a2 s. co m*/
    SQLQueries s = new SQLQueries();

    String lastName = "";

    try {
        lastName = s.getChildSurnameByName(name);
    } catch (SQLException | ClassNotFoundException ex) {
        System.out.println(ex);
    }

    Date date = new Date();
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    String today = df.format(date);
    String fileName = "C:\\creche\\reports\\" + name + today + "_.pdf";

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));

    document.open();

    addTitle(document);
    addLine(document);

    document.add(new Paragraph("Name : " + name + " " + lastName, italic));
    addEmptyLine(document, 2);

    Paragraph paragraph = new Paragraph();
    paragraph.add("Start Date :");
    paragraph.setTabSettings(new TabSettings());
    paragraph.add(Chunk.TABBING);
    paragraph.add(new Chunk(startDate));
    document.add(paragraph);

    addEmptyLine(document, 1);
    document.add(new Paragraph("Account Status : " + accStatus));
    addEmptyLine(document, 1);

    document.add(new Paragraph("Total Amount Outstanding : R" + amountDue));
    addEmptyLine(document, 3);

    addLine(document);
    document.add(new Paragraph("For any queries, please contact Administration : 10111"));

    document.close();
}

From source file:com.wipro.srs.service.PrintTicket.java

@Transactional
public void generatePDF(String res) {
    try {//from   www  . j  a  v a2 s  .  com

        ReservationBean rb = resDao.findByResID(res);

        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();
        addMetaData(document);
        addTitlePage(document);
        addContent(document, rb);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.workhub.utils.PDFUtils.java

public static void createPDF(String title, String creator, List<ElementModel> models,
        FileOutputStream outputStream) {

    try {/*from   w w  w  . ja  v a2  s . c om*/
        Document document = new Document();
        PdfWriter.getInstance(document, outputStream);
        document.open();

        addMetaData(document, title, creator);
        addTitlePage(document, title, models, creator);
        addContent(document, models);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.xhsoft.framework.common.file.PDFHandle.java

License:Open Source License

public static void createPDF(String fileName) {
    Document doc = new Document(PageSize.A4.rotate());
    OutputStream os = null;//  www .ja va 2s.  co  m
    try {
        os = new FileOutputStream(fileName);
        PdfWriter.getInstance(doc, os);
        doc.open();
        doc.add(new Paragraph("Hello World ! "));
        doc.close();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.xumpy.itext.services.TimeSheet.java

public OutputStream pdf(List<Jobs> jobs, OutputStream outputStream)
        throws DocumentException, BadElementException, IOException, URISyntaxException {
    Document document = new Document(PageSize.A4, 0, 0, 160, 220);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();/*from   w  w w .j a  v a2  s .  c om*/

    HeaderFooter event = new HeaderFooter();
    event.setTableHeader(header());
    event.setTableFooter(footer());
    writer.setPageEvent(event);

    document.add(body(jobs));

    document.close();

    return outputStream;
}