Java IText Kütüphanesini Kullanarak HTML İçeriğinden PDF Oluşturmak

07-10-2014
iText kütüphanesi ile HTML içeriğinden PDF dosyası oluşturmak için bir örnek anlatılacaktır.

Örnek:
public static void createPDF(String title, String url, String content) {
    try {
        String html = "<html><head>\n<title>" + url + "</title>\n" +
        
        "</head><body><h2>" + title + "</h2><div class='content'>" + content + "</div></body></html>";
        
        String result = new String(html.getBytes("UTF-8"), "UTF-8");//Unicode karakterlerini desteklemesi icin kullanildi
        InputStream is = new ByteArrayInputStream(result.getBytes());
        FileOutputStream fos = new FileOutputStream(url + ".pdf");
        
        // step 1
        Document document = new Document();
        
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        
        writer.setInitialLeading(12.5f);
        
        // step 3
        document.open();
        
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        
        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        InputStream csspathtest = Thread.currentThread()
        .getContextClassLoader()
        .getResourceAsStream("codesenior.css");
        CssFile cssfiletest = XMLWorkerHelper.getCSS(csspathtest);
        cssResolver.addCss(cssfiletest);
        
        Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
        new HtmlPipeline(htmlContext, new PdfWriterPipeline(
        document, writer)));
        
        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser p = new XMLParser(worker);
        
        p.parse(is);//new FileInputStream("results/demo2/walden.html"));
        
        // step
        document.close();
        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

Css Dosyası
*{
    font-family: Calibri, Helvetica, sans-serif;
}
h2 {
    font: 1.5em Arial,Helvetica,sans-serif;
}
.content {
    background-color: white;
    color: black;
    width: 100%;
    height: 100%;
    font-size: 8pt;
    font-family: Calibri, Helvetica, sans-serif;
}
pre {
    color: #000;
    font-family: "Consolas","Bitstream Vera Sans Mono","Courier New",Courier,monospace !important;
    font-size: 1em !important;
    font-style: normal !important;
    font-weight: normal !important;
    margin: 4px;
}
.important {
    font-weight: bold;
    color: red;
}
.comment {
    color: #808080
}
.annotation {
    font-weight: bold;
    color: #808080;
}
.keyword {
    color: #006699;
    font-weight: bold;
}
#tblContent, #tblContent th, #tblContent td {
    border: 1px solid black;
    border-collapse: collapse;
}
.title {
    font-size: medium;
    color: red
}
.link {
    color: red;
    font-weight: bold;
    text-decoration: none
}
.string {
    color: blue;
}
code {
    white-space: pre-wrap;
    padding-right: 5px;
}
code {
    font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono,
    Courier New, monospace, serif;
    background-color: #EEE;
}

Not: Unicode karakterlerinin desteklenmesi için uygun font seçilmelidir. Biz bu örnekte font-family: Calibri, Helvetica, sans-serif; fontunu seçtik.
Not: Sistem değişkeni Windows işletim sisteminde aşağıdaki gibi düzenlenmelidir:


Maven Dependency

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.lt;/version>
</dependency>
<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.5.lt;/version>
</dependency>

© 2019 Tüm Hakları Saklıdır. Codesenior.COM