从现有的Pdf和(HTML + CSS)创建一个新的Pdf

时间:2015-10-06 17:22:39

标签: java html css pdf itext

我的用例是我正在生成pdf。我也有一个单页的PDF格式。我想在现有的pdf页面之后/之前连接新生成的PDF。

我已经能够从HTML生成PDF(这可能会导致2-3页)Pdf from HTML with CSS

我试着查看其中一个示例,即以页面方式连接现有PDF Working with existing PDFs - Concatenate

2 个答案:

答案 0 :(得分:0)

试试这个例子:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class UtilPDF {

    public static void main(String[] args) {
    try {
        List<InputStream> pdfs = new ArrayList<InputStream>();

        File pdfDir = new File("C:\\PDF");

        boolean pdfDirectoryExists = true;
        if (!pdfDir.exists()) {
            pdfDirectoryExists = pdfDir.mkdir();
        }

        if (pdfDirectoryExists) {
            pdfs.add(new FileInputStream("C:\\PDF\\Document1.pdf"));
            pdfs.add(new FileInputStream("C:\\PDF\\Document2.pdf"));
            OutputStream output = new FileOutputStream("C:\\Projects\\FinalDocument_1_2.pdf");
            UtilPDF.concatPDFs(pdfs, output, true);
        }

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

public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) {
    Document document = new Document();

    try {
        List<InputStream> pdfs = streamOfPDFFiles;
        List<PdfReader> readers = new ArrayList<PdfReader>();
        int totalPages = 0;
        Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        // Create Readers for the pdfs.
        while (iteratorPDFs.hasNext()) {
            InputStream pdf = iteratorPDFs.next();
            PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            totalPages += pdfReader.getNumberOfPages();
        }

        // Create a writer for the outputstream
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        PdfContentByte cb = writer.getDirectContent(); // Holds the PDF

        // data
        PdfImportedPage page;
        int currentPageNumber = 0;
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);

                // Code for pagination.
                if (paginate) {
                    cb.beginText();
                    cb.setFontAndSize(bf, 9);
                    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages,
                            520, 5, 0);
                    cb.endText();
                }
            }
            pageOfCurrentReaderPDF = 0;
        }

        outputStream.flush();
        document.close();
        outputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        if (document.isOpen())
            document.close();

        try {

            if (outputStream != null)
                outputStream.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
}

答案 1 :(得分:0)

此页面准确显示您使用prepend请求的内容,并附加包含HTML和CSS内容的静态PDF。

http://cloudformatter.com/CSS2Pdf.CustomTipsTricks.InjectPDF

使用说明在这里

http://cloudformatter.com/CSS2Pdf.APIDoc.Usage