java - Highlevel interface to PDF-Generation -
i need generate pdf-documents java. i've tried itextpdf, , apachepdfbox. have impression, these libraries possible things pdf.
but have few requirements:
- create pdf-document scratch
- add chapters having title
- adding plain text chapter
- adding text html chapter
- adding image chapter, being scaled fit if needed
- adding table chapter
- create header , footer
- having background image
there quite learning curve these things libraries mentioned above. i'm dreaming of high level api making life easier, having these few methods:
- createchapter(string title)
- addplaintext(chapter chapter,string text)
- addhtml(chapter chapter,string html)
- addimage(chapter chapter,byte[] bytes) (because it's coming out of db)
- addtable(chapter chapter, tablemodel table)
- addheader(headerfootermodel header)
- addfooter(headerfootermodel footer)
- addbackgroundimage(chapter chapter,byte[] bytes)
is there available? cool , safe time.
following class great start @ having high level api itext. implemented of methods requested. remainder left challenge reader.
package stackoverflow; import com.itextpdf.html2pdf.htmlconverter; import com.itextpdf.io.image.imagedatafactory; import com.itextpdf.kernel.color.color; import com.itextpdf.kernel.color.devicergb; import com.itextpdf.kernel.pdf.pdfdocument; import com.itextpdf.kernel.pdf.pdfwriter; import com.itextpdf.layout.document; import com.itextpdf.layout.element.iblockelement; import com.itextpdf.layout.element.ielement; import com.itextpdf.layout.element.image; import com.itextpdf.layout.element.paragraph; import java.io.file; import java.io.ioexception; import java.io.outputstream; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; public class itextfordummiesfacade { // itext io private pdfdocument pdfdocument; private document layoutdocument; // font sizes private float regularfontsize = 12f; private float chaptertitlefontsize = 14f; // font colors private color chapterfontcolor = new devicergb(249, 157, 37); private color regularfontcolor = new devicergb(100, 100, 100); // structure private map<string, integer> chapternames = new hashmap<>(); private map<integer, list<ielement>> elementsperchapter = new hashmap<>(); public itextfordummiesfacade(outputstream os) throws ioexception { this.pdfdocument = new pdfdocument(new pdfwriter(os)); this.layoutdocument = new document(pdfdocument); } public itextfordummiesfacade(file outputfile) throws ioexception { this.pdfdocument = new pdfdocument(new pdfwriter(outputfile)); this.layoutdocument = new document(pdfdocument); } public boolean createchapter(string title) { if(chapternames.containskey(title)) return false; int nextid = chapternames.size(); chapternames.put(title, nextid); elementsperchapter.put(nextid, new arraylist<ielement>()); elementsperchapter.get(nextid).add(new paragraph(title) .setfontsize(chaptertitlefontsize) .setfontcolor(chapterfontcolor)); return true; } public boolean addplaintext(string chapter, string text) { if(!chapternames.containskey(chapter)) return false; int id = chapternames.get(chapter); elementsperchapter.get(id).add(new paragraph(text) .setfontsize(regularfontsize) .setfontcolor(regularfontcolor)); return true; } public boolean addhtml(string chapter, string html) { if(!chapternames.containskey(chapter)) return false; int id = chapternames.get(chapter); try { elementsperchapter.get(id).addall(htmlconverter.converttoelements(html)); } catch (ioexception e) { e.printstacktrace(); return false; } return true; } public boolean addimage(string chapter, byte[] image) { if(!chapternames.containskey(chapter)) return false; int id = chapternames.get(chapter); elementsperchapter.get(id).add(new image(imagedatafactory.create(image))); return true; } private void write() { for(int i=0;i<chapternames.size();i++) { for(ielement e : elementsperchapter.get(i)) if(e instanceof iblockelement) layoutdocument.add((iblockelement) e); } } public void close() { write(); layoutdocument.flush(); layoutdocument.close(); } }
you can call facade work you.
file outputfile = new file(system.getproperty("user.home"), "output.pdf"); itextfordummiesfacade facade = new itextfordummiesfacade(outputfile); facade.createchapter("chapter 1"); facade.addplaintext("chapter 1","lorem ipsum dummy text of printing , typesetting industry. lorem ipsum has been industry's standard dummy text ever since 1500s, when unknown printer took galley of type , scrambled make type specimen book. has survived not 5 centuries, leap electronic typesetting, remaining unchanged. popularised in 1960s release of letraset sheets containing lorem ipsum passages, , more desktop publishing software aldus pagemaker including versions of lorem ipsum."); facade.close();
Comments
Post a Comment