Search in sources :

Example 1 with ConverterProperties

use of com.itextpdf.html2pdf.ConverterProperties in project MessdienerPlanErsteller by Aclrian.

the class FinishController method toPDF.

@FXML
public void toPDF(ActionEvent actionEvent) {
    if (LocalOfficeUtils.getDefaultOfficeHome() != null) {
        Log.getLogger().info("Converting HTML to PDF with JODConverter");
        convert(actionEvent, false);
    }
    Log.getLogger().info("Converting HTML to PDF with iText");
    ConverterProperties converterProperties = new ConverterProperties();
    converterProperties.setCharset("UTF-8");
    try {
        File out = new File(Log.getWorkingDir().getAbsolutePath(), titel + ".pdf");
        HtmlConverter.convertToPdf(new ByteArrayInputStream(editor.getHtmlText().replace("<p></p>", "<br>").replace("\u2003", "    ").getBytes(StandardCharsets.UTF_8)), new FileOutputStream(out), converterProperties);
        pdfgen = out;
        if (actionEvent != null) {
            Desktop.getDesktop().open(out);
        }
    } catch (IOException e) {
        Dialogs.getDialogs().error(e, "Konnte den Messdienerplan nicht zu PDF konvertieren.");
    }
}
Also used : ConverterProperties(com.itextpdf.html2pdf.ConverterProperties) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FXML(javafx.fxml.FXML)

Example 2 with ConverterProperties

use of com.itextpdf.html2pdf.ConverterProperties in project axelor-open-suite by axelor.

the class PrintServiceImpl method generatePDF.

@Override
@Transactional
public Map<String, Object> generatePDF(Print print) throws AxelorException {
    try {
        print = printRepo.find(print.getId());
        String html = generateHtml(print);
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        try (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfOutputStream))) {
            pdfDoc.setDefaultPageSize(print.getDisplayTypeSelect() == PrintRepository.DISPLAY_TYPE_LANDSCAPE ? PageSize.A4.rotate() : PageSize.A4);
            if (print.getPrintPdfFooter() != null && !print.getHidePrintSettings()) {
                com.itextpdf.layout.Document doc = new com.itextpdf.layout.Document(pdfDoc);
                pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TableFooterEventHandler(doc, print));
            }
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setBaseUri(attachmentPath);
            HtmlConverter.convertToPdf(html, pdfDoc, converterProperties);
        }
        String documentName = (StringUtils.notEmpty(print.getDocumentName()) ? print.getDocumentName() : "") + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + FILE_EXTENSION_PDF;
        InputStream pdfInputStream = new ByteArrayInputStream(pdfOutputStream.toByteArray());
        MetaFile metaFile = metaFiles.upload(pdfInputStream, documentName);
        File file = MetaFiles.getPath(metaFile).toFile();
        String fileLink = PdfTool.getFileLinkFromPdfFile(PdfTool.printCopiesToFile(file, 1), metaFile.getFileName());
        if (ObjectUtils.notEmpty(file) && file.exists() && (print.getAttach() || StringUtils.notEmpty(print.getMetaFileField()))) {
            Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(print.getMetaModel().getFullName());
            Model objectModel = JPA.find(modelClass, print.getObjectId());
            if (ObjectUtils.notEmpty(objectModel)) {
                if (print.getAttach()) {
                    metaFiles.attach(metaFile, documentName, objectModel);
                }
                if (StringUtils.notEmpty(print.getMetaFileField())) {
                    saveMetaFileInModel(modelClass, objectModel, metaFile, print.getMetaFileField());
                }
            }
        }
        if (CollectionUtils.isNotEmpty(print.getPrintSet())) {
            for (Print subPrint : print.getPrintSet()) {
                generatePDF(subPrint);
            }
        }
        return ActionView.define(documentName).add("html", fileLink).map();
    } catch (IOException | ClassNotFoundException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_INCONSISTENCY);
    }
}
Also used : ConverterProperties(com.itextpdf.html2pdf.ConverterProperties) AxelorException(com.axelor.exception.AxelorException) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Print(com.axelor.apps.base.db.Print) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(com.axelor.db.Model) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Example 3 with ConverterProperties

use of com.itextpdf.html2pdf.ConverterProperties in project mycore by MyCoRe-Org.

the class MCRXHTML2PDFTransformer method transform.

@Override
public MCRContent transform(MCRContent source) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String s = source.asString();
    // required because some data is not normalized
    s = Normalizer.normalize(s, Normalizer.Form.NFC);
    // required because itext cant handle these
    s = s.replace(" ", "");
    ConverterProperties converterProperties = new ConverterProperties();
    converterProperties.setFontProvider(lazyInitFontProvider());
    HtmlConverter.convertToPdf(s, baos, converterProperties);
    return new MCRByteContent(baos.toByteArray());
}
Also used : ConverterProperties(com.itextpdf.html2pdf.ConverterProperties) MCRByteContent(org.mycore.common.content.MCRByteContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with ConverterProperties

use of com.itextpdf.html2pdf.ConverterProperties in project commons by mosip.

the class PDFGeneratorImpl method generate.

/*
	 * (non-Javadoc)
	 * 
	 * @see io.mosip.kernel.core.pdfgenerator.spi.PDFGenerator#generate(java.io.
	 * InputStream, java.lang.String)
	 */
@Override
public OutputStream generate(InputStream is, String resourceLoc) throws IOException {
    isValidInputStream(is);
    OutputStream os = new ByteArrayOutputStream();
    PdfWriter pdfWriter = new PdfWriter(os);
    PdfDocument pdfDoc = new PdfDocument(pdfWriter);
    ConverterProperties converterProperties = new ConverterProperties();
    pdfDoc.setTagged();
    PageSize pageSize = PageSize.A4.rotate();
    pdfDoc.setDefaultPageSize(pageSize);
    float screenWidth = CssUtils.parseAbsoluteLength("" + pageSize.getWidth());
    MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
    mediaDescription.setWidth(screenWidth);
    DefaultFontProvider dfp = new DefaultFontProvider(true, true, false);
    converterProperties.setMediaDeviceDescription(mediaDescription);
    converterProperties.setFontProvider(dfp);
    converterProperties.setBaseUri(resourceLoc);
    converterProperties.setCreateAcroForm(true);
    try {
        HtmlConverter.convertToPdf(is, pdfDoc, converterProperties);
    } catch (Exception e) {
        throw new PDFGeneratorException(PDFGeneratorExceptionCodeConstant.PDF_EXCEPTION.getErrorCode(), e.getMessage());
    }
    return os;
}
Also used : ConverterProperties(com.itextpdf.html2pdf.ConverterProperties) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PageSize(com.itextpdf.kernel.geom.PageSize) DefaultFontProvider(com.itextpdf.html2pdf.resolver.font.DefaultFontProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MediaDeviceDescription(com.itextpdf.html2pdf.css.media.MediaDeviceDescription) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) GeneralSecurityException(java.security.GeneralSecurityException) PDFGeneratorException(io.mosip.kernel.core.pdfgenerator.exception.PDFGeneratorException) IOException(java.io.IOException) DocumentException(com.itextpdf.text.DocumentException) PDFGeneratorException(io.mosip.kernel.core.pdfgenerator.exception.PDFGeneratorException)

Example 5 with ConverterProperties

use of com.itextpdf.html2pdf.ConverterProperties in project Rolls-Royce by EntryDSM.

the class ConverterPropertiesCreator method createConverterProperties.

public ConverterProperties createConverterProperties() {
    ConverterProperties properties = new ConverterProperties();
    FontProvider fontProvider = new DefaultFontProvider(false, false, false);
    fonts.forEach(font -> {
        try {
            FontProgram fontProgram = FontProgramFactory.createFont(fontPath + font);
            fontProvider.addFont(fontProgram);
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
    properties.setFontProvider(fontProvider);
    return properties;
}
Also used : ConverterProperties(com.itextpdf.html2pdf.ConverterProperties) DefaultFontProvider(com.itextpdf.html2pdf.resolver.font.DefaultFontProvider) IOException(java.io.IOException) DefaultFontProvider(com.itextpdf.html2pdf.resolver.font.DefaultFontProvider) FontProvider(com.itextpdf.layout.font.FontProvider) FontProgram(com.itextpdf.io.font.FontProgram)

Aggregations

ConverterProperties (com.itextpdf.html2pdf.ConverterProperties)5 IOException (java.io.IOException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DefaultFontProvider (com.itextpdf.html2pdf.resolver.font.DefaultFontProvider)2 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)2 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 Print (com.axelor.apps.base.db.Print)1 Model (com.axelor.db.Model)1 AxelorException (com.axelor.exception.AxelorException)1 MetaFile (com.axelor.meta.db.MetaFile)1 Transactional (com.google.inject.persist.Transactional)1 MediaDeviceDescription (com.itextpdf.html2pdf.css.media.MediaDeviceDescription)1 FontProgram (com.itextpdf.io.font.FontProgram)1 PageSize (com.itextpdf.kernel.geom.PageSize)1 FontProvider (com.itextpdf.layout.font.FontProvider)1 DocumentException (com.itextpdf.text.DocumentException)1 PDFGeneratorException (io.mosip.kernel.core.pdfgenerator.exception.PDFGeneratorException)1 FileOutputStream (java.io.FileOutputStream)1