Java Code Examples for org.xhtmlrenderer.pdf.ITextRenderer#createPDF()

The following examples show how to use org.xhtmlrenderer.pdf.ITextRenderer#createPDF() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDFThymeleafExample.java    From tutorials with MIT License 5 votes vote down vote up
public void generatePdfFromHtml(String html) throws IOException, DocumentException {
    String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf";
    OutputStream outputStream = new FileOutputStream(outputFolder);

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    renderer.createPDF(outputStream);

    outputStream.close();
}
 
Example 2
Source File: GeneratePDFWorkitemHandler.java    From jbpm-work-items with Apache License 2.0 4 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager workItemManager) {

    try {

        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        Map<String, Object> results = new HashMap<>();
        String templateXHTML = (String) workItem.getParameter("TemplateXHTML");
        String pdfName = (String) workItem.getParameter("PDFName");

        if (pdfName == null || pdfName.isEmpty()) {
            pdfName = "generatedpdf";
        }

        Configuration cfg = new Configuration(freemarker.template.Configuration.VERSION_2_3_26);
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setLogTemplateExceptions(false);

        StringTemplateLoader stringLoader = new StringTemplateLoader();
        stringLoader.putTemplate("pdfTemplate",
                                 templateXHTML);
        cfg.setTemplateLoader(stringLoader);

        StringWriter stringWriter = new StringWriter();

        Template pdfTemplate = cfg.getTemplate("pdfTemplate");
        pdfTemplate.process(workItem.getParameters(),
                            stringWriter);
        resultXHTML = stringWriter.toString();

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(resultXHTML);
        renderer.layout();

        Document document = new DocumentImpl();
        document.setName(pdfName + ".pdf");
        document.setLastModified(new Date());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        renderer.createPDF(baos);
        document.setContent(baos.toByteArray());

        results.put(RESULTS_VALUE,
                    document);

        workItemManager.completeWorkItem(workItem.getId(),
                                         results);
    } catch (Exception e) {
        logger.error(e.getMessage());
        handleException(e);
    }
}
 
Example 3
Source File: cfDOCUMENT.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
private void preparePageCounters( cfSession _Session, ITextRenderer _renderer, List<DocumentSection> _sections, DocumentSettings _settings ) throws cfmRunTimeException{
	OutputStream pdfOut = null;
	try{
		pdfOut = new NullOutputStream();
		
		DocumentSection nextSection = _sections.get( 0 );
		if (nextSection.pageCounterConflict())
			throw newRunTimeException("OpenBD doesn't support currentpagenumber and currentsectionpagenumber in same section.");		
		String renderedBody = getRenderedBody( _Session, nextSection, _settings, _sections.size() );
		_renderer.setDocument( getDocument( renderedBody ), nextSection.getBaseUrl( _Session ) );
		_renderer.layout();
		_renderer.createPDF( pdfOut, false );
		
		int currentPageNumber = _renderer.getWriter().getCurrentPageNumber();
		nextSection.setTotalSectionPageCount(currentPageNumber);
		int totalPageCount = currentPageNumber;

		for ( int i = 1; i < _sections.size(); i++ ){
			nextSection = _sections.get( i );
			if (nextSection.pageCounterConflict())
				throw newRunTimeException("OpenBD doesn't support currentpagenumber and currentsectionpagenumber in same section.");
			renderedBody = getRenderedBody( _Session, nextSection, _settings, _sections.size() );
			_renderer.setDocument( getDocument( renderedBody ), nextSection.getBaseUrl( _Session ) );
			_renderer.layout();
			_renderer.writeNextDocument( _renderer.getWriter().getCurrentPageNumber()+1 );
			currentPageNumber = _renderer.getWriter().getCurrentPageNumber();
			nextSection.setTotalSectionPageCount(currentPageNumber-totalPageCount);
			totalPageCount = currentPageNumber;
		}
		
		for ( int i = 0; i < _sections.size(); i++ ){
			nextSection = _sections.get( i );
			nextSection.setTotalPageCount(totalPageCount);
		}
	
	} catch (DocumentException e) {
		throw newRunTimeException( "Failed to create PDF due to DocumentException: " + e.getMessage() );
	}finally{
		if ( pdfOut != null )try{ pdfOut.close(); }catch( IOException ignored ){}
	}
}
 
Example 4
Source File: PDFThymeleafUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
private ByteArrayOutputStream generatePdfOutputStreamFromHtml(String html) throws IOException, DocumentException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    renderer.createPDF(outputStream);

    outputStream.close();
    return outputStream;
}