pdf.DailyReportPDF.java Source code

Java tutorial

Introduction

Here is the source code for pdf.DailyReportPDF.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pdf;

import dto.Product;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.rendering.PDFRenderer;
import static pdf.WorkOrderPDF.fillInfo;

/**
 *
 * @author Casper
 */
public class DailyReportPDF {

    private static File template = new File("pdf_docs\\templates\\dnevni_izvjestaj_template.pdf");
    private Date dateOfDailyReport;
    private ArrayList<Product> products = new ArrayList();
    private static PDPageContentStream contentStream;
    private double sumProductPrice;

    public DailyReportPDF() {
    }

    public DailyReportPDF(ArrayList<Product> products, Date dateOfDailyReport) throws IOException {
        this.products = products;
        this.dateOfDailyReport = dateOfDailyReport;
        sumProductPrice = 0;
        //createDailyReport();
    }

    public void createDailyReportPNG(String path) throws IOException {
        File file = new File(path);
        PDDocument doc = PDDocument.load(file);
        PDFRenderer renderer = new PDFRenderer(doc);
        BufferedImage image = renderer.renderImage(0);
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
        ImageIO.write(image, "PNG",
                new File("pdf_docs\\daily_reports_picture\\" + "drpic-" + sdf.format(dateOfDailyReport) + ".png"));
        doc.close();
    }

    public void insertProducts() throws IOException {
        int row = 0;
        int column = 0;
        int offsetRow = 20;
        int offsetColumn = 353;
        int delay = 2;
        sumProductPrice = 0;
        for (Product pr : products) {
            if (row < 5) {
                fillTables(pr.getSelector(), 78 + (column * offsetColumn), 426 - (row * offsetRow), contentStream);
                fillTables(pr.getMeasureUnit(), 196 + (column * offsetColumn), 426 - (row * offsetRow),
                        contentStream);
                fillTables(Double.toString(pr.getTempQuantity()), 234 + (column * offsetColumn),
                        426 - (row * offsetRow), contentStream);
                fillTables(Double.toString(pr.getTempPrice()), 286 + (column * offsetColumn),
                        426 - (row * offsetRow), contentStream);
                fillTables(Double.toString(pr.getTempQuantity() * pr.getTempPrice()), 341 + (column * offsetColumn),
                        426 - (row * offsetRow), contentStream);
                fillTables(pr.getFormatName(), 400 + (column * offsetColumn), 426 - (row * offsetRow),
                        contentStream);
                contentStream.close();
            } else {
                fillTables(pr.getSelector(), 78 + (column * offsetColumn), 426 - (row * offsetRow) - delay,
                        contentStream);
                fillTables(pr.getMeasureUnit(), 196 + (column * offsetColumn), 426 - (row * offsetRow) - delay,
                        contentStream);
                fillTables(Double.toString(pr.getTempQuantity()), 234 + (column * offsetColumn),
                        426 - (row * offsetRow) - delay, contentStream);
                fillTables(Double.toString(pr.getTempPrice()), 286 + (column * offsetColumn),
                        426 - (row * offsetRow) - delay, contentStream);
                fillTables(Double.toString(pr.getTempQuantity() * pr.getTempPrice()), 341 + (column * offsetColumn),
                        426 - (row * offsetRow) - delay, contentStream);
                fillTables(pr.getFormatName(), 400 + (column * offsetColumn), 426 - (row * offsetRow),
                        contentStream);
                contentStream.close();
                delay += 1;
            }
            if (row == 10) {
                delay -= 3;
            }
            sumProductPrice += pr.getTempQuantity() * pr.getTempPrice();

            row = ++row % 20;
        }
    }

    public void dailyReportInfo() throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");

        fillInfo(sdf.format(dateOfDailyReport), 120, 510, contentStream);
        DecimalFormat df = new DecimalFormat("####0.00");
        fillInfo(df.format(sumProductPrice), 750, 40, contentStream);
        contentStream.close();

    }

    public String createDailyReport() throws IOException {
        PDDocument document = PDDocument.load(template);
        //prva strana
        PDPage page = document.getPage(0);
        contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

        this.insertProducts();
        this.dailyReportInfo();
        contentStream.close();
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
        String path = "pdf_docs\\daily_reports\\" + "daily_report_" + dateOfDailyReport.getTime() + ".pdf";
        document.save(path);
        //createDailyReportPNG(path);
        document.close();

        return path;
    }

    public static void fillTables(String text, int x, int y, PDPageContentStream contentStream) {
        try {
            contentStream.beginText();

            contentStream.setFont(PDType1Font.TIMES_ROMAN, 10);
            contentStream.newLineAtOffset(x, y);
            if (text != null) {
                contentStream.showText(text);
            }
            contentStream.endText();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void fillInfo(String text, int x, int y, PDPageContentStream contentStream) {
        try {

            contentStream.beginText();

            contentStream.setFont(PDType1Font.TIMES_BOLD, 16);
            contentStream.newLineAtOffset(x, y);
            contentStream.showText(text);

            contentStream.endText();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}