diff --git a/README.md b/README.md index f9135ee2e..e472eb7c8 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ There are five kinds of components (each components README.md contains module st * [Apache POI Module (fj-doc-mod-poi)](fj-doc-mod-poi/README.md) ([XLS](fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/XlsPoiTypeHandler.java)/[XLSX](fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/XlsPoiTypeHandler.java)) * [Apache FOP Module (fj-doc-mod-fop)](fj-doc-mod-fop/README.md) ([PDF](fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/PdfFopTypeHandler.java)/[FO](fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/FreeMarkerFopTypeHandler.java)) * [OpenCSV Module (fj-doc-mod-opencsv)](fj-doc-mod-opencsv/README.md) ([CSV](fj-doc-mod-opencsv/src/main/java/org/fugerit/java/doc/mod/opencsv/OpenCSVTypeHandler.java)) +* [OpenPDF EXT Module (fj-doc-mod-openpdf-ext)](fj-doc-mod-openpdf-ext/README.md) ([PDF](fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/PdfTypeHandler.java)/[HTML](fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/HtmlTypeHandler.java)) +* [OpenRTF EXT Module (fj-doc-mod-openrtf-ext)](fj-doc-mod-openrtf-ext/README.md) ([RTF](fj-doc-mod-openrtf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/RtfTypeHandler.java)) ### 3. Available type handlers : @@ -102,6 +104,10 @@ There are five kinds of components (each components README.md contains module st * [PDF](fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/PdfFopTypeHandler.java) - (fj-doc-mod-fop) - output as PDF using Apache FOP * [FO](fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/FreeMarkerFopTypeHandler.java) - (fj-doc-mod-fop) - output as FO using Apache FOP * [CSV](fj-doc-mod-opencsv/src/main/java/org/fugerit/java/doc/mod/opencsv/OpenCSVTypeHandler.java) - (fj-doc-mod-opencsv) - output as CSV using OpenCSV +* [PDF](fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/PdfTypeHandler.java) - (fj-doc-mod-openpdf-ext) - output as PDF using OpenPDF +* [HTML](fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/HtmlTypeHandler.java) - (fj-doc-mod-openpdf-ext) - output as HTML using OpenPDF +* [RTF](fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/RtfTypeHandler.java) - (fj-doc-mod-openrtf-ext) - output as RTF using OpenRTF + ### 4. Bundled libraries : * [Simple table (fj-doc-lib-singletable)](fj-doc-lib-simpletable/README.md) - offers a simple API for creating a document made of a table. diff --git a/fj-doc-mod-opencsv/README.md b/fj-doc-mod-opencsv/README.md index 5b86c390f..c1007af65 100644 --- a/fj-doc-mod-opencsv/README.md +++ b/fj-doc-mod-opencsv/README.md @@ -9,7 +9,7 @@ *Status* : Basic features implemented. (Sample JUnit [TestFreeMarker01](../fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/TestFreeMarker01.java) is now working). -For the intrinsic limitations of the CSV format, is possible to choose a sinlge table in the document and outputs it as CSV. +For the intrinsic limitations of the CSV format, is possible to choose a single table in the document and outputs it as CSV. *Since* : fj-doc 0.6 diff --git a/fj-doc-mod-opencsv/pom.xml b/fj-doc-mod-opencsv/pom.xml index ba12aaddb..1825adb60 100644 --- a/fj-doc-mod-opencsv/pom.xml +++ b/fj-doc-mod-opencsv/pom.xml @@ -11,7 +11,7 @@ fj-doc-mod-opencsv - Rendere for CSV using OpenCSV + Renderer for CSV using OpenCSV diff --git a/fj-doc-mod-openpdf-ext/README.md b/fj-doc-mod-openpdf-ext/README.md new file mode 100644 index 000000000..e0667abea --- /dev/null +++ b/fj-doc-mod-openpdf-ext/README.md @@ -0,0 +1,22 @@ +# Fugerit Document Generation Framework (fj-doc) + +## OpenPDF Renderer (PDF)(fj-doc-mod-openpdf-ext) + +[back to fj-doc index](../README.md) + +[![Maven Central](https://img.shields.io/maven-central/v/org.fugerit.java/fj-doc-mod-openpdf-ext.svg)](https://mvnrepository.com/artifact/org.fugerit.java/fj-doc-mod-openpdf-ext) +[![javadoc](https://javadoc.io/badge2/org.fugerit.java/fj-doc-mod-openpdf-ext/javadoc.svg)](https://javadoc.io/doc/org.fugerit.java/fj-doc-mod-openpdf-ext) + +Renderer for PDF format, based on [OpenPDF](https://github.com/LibrePDF/OpenPDF). + +*Status* : +Basic features implemented. (Sample JUnit [TestFreeMarker01](../fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/TestFreeMarker01.java) is now working). + +*Since* : fj-doc 3.4 + +*Native support* : +Disabled, native support will be added in a future release. openpdf-ext native support must be verified. + +*Quickstart* : +Basically this is only a type handler, see core library [fj-doc-base](../fj-doc-base/README.md). +NOTE: If you have any special need you can open a pull request or create your own handler based on this. \ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/pom.xml b/fj-doc-mod-openpdf-ext/pom.xml new file mode 100644 index 000000000..bbfb4de02 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + + fj-doc-mod-openpdf-ext + + + org.fugerit.java + fj-doc + 3.3.2-SNAPSHOT + + + fj-doc-mod-openpdf-ext + Renderer for PDF format, based on https://github.com/LibrePDF/OpenPDF + + + 1.3.39 + + + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + + com.github.librepdf + openpdf + ${openpdf-version-version} + + + + org.fugerit.java + fj-core + + + + org.fugerit.java + fj-doc-base + + + + org.fugerit.java + fj-doc-freemarker + test + + + + + + https://www.fugerit.org + Fugerit + + + https://www.fugerit.org/perm/venus/ + + diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/HtmlTypeHandler.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/HtmlTypeHandler.java new file mode 100644 index 000000000..57da45c8c --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/HtmlTypeHandler.java @@ -0,0 +1,53 @@ +package org.fugerit.java.doc.mod.openpdf.ext; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +import org.fugerit.java.core.cfg.ConfigException; +import org.fugerit.java.doc.base.config.DocConfig; +import org.fugerit.java.doc.base.config.DocInput; +import org.fugerit.java.doc.base.config.DocOutput; +import org.fugerit.java.doc.base.config.DocTypeHandler; +import org.fugerit.java.doc.base.config.DocTypeHandlerDefault; +import org.fugerit.java.doc.base.model.DocBase; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPDFConfigHelper; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPpfDocHandler; +import org.w3c.dom.Element; + +import com.lowagie.text.Document; +import com.lowagie.text.html.HtmlWriter; + +public class HtmlTypeHandler extends DocTypeHandlerDefault { + + /** + * + */ + private static final long serialVersionUID = -3491991891783269389L; + + public static final DocTypeHandler HANDLER = new HtmlTypeHandler(); + + public HtmlTypeHandler() { + super( DocConfig.TYPE_HTML, OpenPpfDocHandler.MODULE ); + } + + @Override + public void handle(DocInput docInput, DocOutput docOutput) throws Exception { + DocBase docBase = docInput.getDoc(); + OutputStream outputStream = docOutput.getOs(); + Document document = new Document( ); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + HtmlWriter.getInstance( document, baos ); + OpenPpfDocHandler handler = new OpenPpfDocHandler( document, DocConfig.TYPE_HTML ); + handler.handleDoc( docBase ); + baos.writeTo( outputStream ); + baos.close(); + outputStream.close(); + } + + @Override + protected void handleConfigTag(Element config) throws ConfigException { + super.handleConfigTag(config); + OpenPDFConfigHelper.handleConfig( config, this.getType() ); + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/PdfTypeHandler.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/PdfTypeHandler.java new file mode 100644 index 000000000..4264c2ee3 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/PdfTypeHandler.java @@ -0,0 +1,71 @@ +package org.fugerit.java.doc.mod.openpdf.ext; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +import org.fugerit.java.core.cfg.ConfigException; +import org.fugerit.java.doc.base.config.DocConfig; +import org.fugerit.java.doc.base.config.DocInput; +import org.fugerit.java.doc.base.config.DocOutput; +import org.fugerit.java.doc.base.config.DocTypeHandler; +import org.fugerit.java.doc.base.config.DocTypeHandlerDefault; +import org.fugerit.java.doc.base.model.DocBase; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPDFConfigHelper; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPpfDocHandler; +import org.w3c.dom.Element; + +import com.lowagie.text.Document; +import com.lowagie.text.PageSize; +import com.lowagie.text.pdf.PdfWriter; + +public class PdfTypeHandler extends DocTypeHandlerDefault { + + /** + * + */ + private static final long serialVersionUID = 5459938865782356227L; + + public static final DocTypeHandler HANDLER = new PdfTypeHandler(); + + public PdfTypeHandler() { + super( DocConfig.TYPE_PDF, OpenPpfDocHandler.MODULE ); + } + + @Override + public void handle(DocInput docInput, DocOutput docOutput) throws Exception { + DocBase docBase = docInput.getDoc(); + OutputStream outputStream = docOutput.getOs(); + String[] margins = docBase.getInfo().getProperty( "margins", "20;20;20;20" ).split( ";" ); + Document document = new Document( PageSize.A4, Integer.parseInt( margins[0] ), + Integer.parseInt( margins[1] ), + Integer.parseInt( margins[2] ), + Integer.parseInt( margins[3] ) ); + // allocate buffer + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // create pdf writer + PdfWriter pdfWriter = PdfWriter.getInstance( document, baos ); + // create doc handler + OpenPpfDocHandler handler = new OpenPpfDocHandler( document, pdfWriter ); + if ( "true".equalsIgnoreCase( docBase.getInfo().getProperty( "set-total-page" ) ) ) { + handler.handleDoc( docBase ); + int totalPageCount = pdfWriter.getCurrentPageNumber()-1; + document = new Document( PageSize.A4, Integer.parseInt( margins[0] ), + Integer.parseInt( margins[1] ), + Integer.parseInt( margins[2] ), + Integer.parseInt( margins[3] ) ); + baos = new ByteArrayOutputStream(); + pdfWriter = PdfWriter.getInstance( document, baos ); + handler = new OpenPpfDocHandler(document, pdfWriter, totalPageCount ); + } + handler.handleDoc( docBase ); + baos.writeTo( outputStream ); + baos.close(); + outputStream.close(); + } + + @Override + protected void handleConfigTag(Element config) throws ConfigException { + super.handleConfigTag(config); + OpenPDFConfigHelper.handleConfig( config, this.getType() ); + } +} diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/CellParent.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/CellParent.java new file mode 100644 index 000000000..eff559c19 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/CellParent.java @@ -0,0 +1,21 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import com.lowagie.text.Cell; +import com.lowagie.text.Element; + +public class CellParent implements ParentElement { + + private Cell cell; + + public CellParent( Cell cell ) { + this.cell = cell; + } + + /* (non-Javadoc) + * @see org.fugerit.java.doc.mod.itext.ParentElement#add(com.lowagie.text.Element) + */ + public void add(Element element) { + this.cell.addElement( element ); + } + +} \ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/DocumentParent.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/DocumentParent.java new file mode 100644 index 000000000..cc5af99f7 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/DocumentParent.java @@ -0,0 +1,21 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import com.lowagie.text.Document; +import com.lowagie.text.Element; + +public class DocumentParent implements ParentElement { + + private Document document; + + public DocumentParent( Document document) { + this.document = document; + } + + /* (non-Javadoc) + * @see org.fugerit.java.doc.mod.itext.ParentElement#add(com.lowagie.text.Element) + */ + public void add(Element element) { + this.document.add( element ); + } + +} \ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPDFConfigHelper.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPDFConfigHelper.java new file mode 100644 index 000000000..d7af531d4 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPDFConfigHelper.java @@ -0,0 +1,32 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import org.fugerit.java.core.cfg.ConfigException; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class OpenPDFConfigHelper { + + private OpenPDFConfigHelper() {} + + public static final String TAG_NAME_FONT = "font"; + + public static final String ATT_NAME = "name"; + public static final String ATT_PATH = "path"; + + + public static void handleConfig( Element config, String type ) throws ConfigException { + log.info( "configure for type: {}", type ); + NodeList fontList = config.getElementsByTagName( TAG_NAME_FONT ); + for ( int k=0; k OpenPpfDocHandler.registerFont( name , path ) ); + } + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfDocTableHelper.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfDocTableHelper.java new file mode 100644 index 000000000..9e0620451 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfDocTableHelper.java @@ -0,0 +1,198 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import java.awt.Color; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; + +import org.fugerit.java.doc.base.model.DocBarcode; +import org.fugerit.java.doc.base.model.DocBorders; +import org.fugerit.java.doc.base.model.DocCell; +import org.fugerit.java.doc.base.model.DocElement; +import org.fugerit.java.doc.base.model.DocImage; +import org.fugerit.java.doc.base.model.DocPara; +import org.fugerit.java.doc.base.model.DocPhrase; +import org.fugerit.java.doc.base.model.DocRow; +import org.fugerit.java.doc.base.model.DocTable; +import org.fugerit.java.doc.base.xml.DocModelUtils; + +import com.lowagie.text.Cell; +import com.lowagie.text.Chunk; +import com.lowagie.text.DocumentException; +import com.lowagie.text.Element; +import com.lowagie.text.Font; +import com.lowagie.text.Paragraph; +import com.lowagie.text.Table; +import com.lowagie.text.alignment.HorizontalAlignment; +import com.lowagie.text.alignment.VerticalAlignment; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class OpenPdfDocTableHelper { + + private OpenPdfDocTableHelper() {} + + private static void handleBorderColor( DocBorders docBorders, Cell cell ) { + if ( docBorders.getBorderColorBottom() != null ) { + cell.setBorderColorBottom( DocModelUtils.parseHtmlColor( docBorders.getBorderColorBottom() ) ); + } + if ( docBorders.getBorderColorTop() != null ) { + cell.setBorderColorTop( DocModelUtils.parseHtmlColor( docBorders.getBorderColorTop() ) ); + } + if ( docBorders.getBorderColorLeft() != null ) { + cell.setBorderColorLeft( DocModelUtils.parseHtmlColor( docBorders.getBorderColorLeft() ) ); + } + if ( docBorders.getBorderColorRight() != null ) { + cell.setBorderColorRight( DocModelUtils.parseHtmlColor( docBorders.getBorderColorRight() ) ); + } + } + + private static void handleBorderWidth( DocBorders docBorders, Cell cell ) { + if ( docBorders.getBorderWidthBottom() != -1 ) { + cell.setBorderWidthBottom( docBorders.getBorderWidthBottom() ); + } + if ( docBorders.getBorderWidthTop() != -1 ) { + cell.setBorderWidthTop( docBorders.getBorderWidthTop() ); + } + if ( docBorders.getBorderWidthLeft() != -1 ) { + cell.setBorderWidthLeft( docBorders.getBorderWidthLeft() ); + } + if ( docBorders.getBorderWidthRight() != -1 ) { + cell.setBorderWidthRight( docBorders.getBorderWidthRight() ); + } + } + + private static void handleBolders( DocCell docCell, Cell cell ) { + DocBorders docBorders = docCell.getDocBorders(); + if ( docBorders != null ) { + handleBorderColor(docBorders, cell); + handleBorderWidth(docBorders, cell); + } + } + + private static void handleAligns( DocCell docCell, Cell cell ) { + if ( docCell.getAlign() != DocPara.ALIGN_UNSET ) { + Optional ha = HorizontalAlignment.of( docCell.getAlign() ); + if ( ha.isPresent() ) { + cell.setHorizontalAlignment( ha.get() ); + } + } + if ( docCell.getValign() != DocPara.ALIGN_UNSET ) { + Optional va = VerticalAlignment.of( docCell.getValign() ); + if ( va.isPresent() ) { + cell.setVerticalAlignment( va.get() ); + } + } + } + + private static List handleContent( Table table, CellParent cellParent, DocCell docCell, Cell cell, OpenPdfHelper docHelper ) throws DocumentException, IOException { + List fontList = new ArrayList<>(); + Iterator itCurrent = docCell.docElements(); + while ( itCurrent.hasNext() ) { + DocElement docElement = itCurrent.next(); + if ( docElement instanceof DocPara ) { + DocPara docPara = (DocPara)docElement; + OpenPpfDocHandler.setStyle( docCell , docPara ); + Paragraph paragraph = OpenPpfDocHandler.createPara( docPara, docHelper, fontList ); + cellParent.add( paragraph ); + } else if ( docElement instanceof DocPhrase ) { + DocPhrase docPhrase = (DocPhrase)docElement; + log.trace( "docCell -> {}, docPara : {}", docCell, cell ); + cellParent.add( OpenPpfDocHandler.createPhrase( docPhrase, docHelper, fontList ) ); + } else if ( docElement instanceof DocTable ) { + table.insertTable( createTable( (DocTable)docElement, docHelper ) ); + } else if ( docElement instanceof DocImage ) { + cellParent.add( OpenPpfDocHandler.createImage( (DocImage)docElement ) ); + } else if ( docElement instanceof DocBarcode ) { + cellParent.add( OpenPpfDocHandler.createBarcode( (DocBarcode)docElement, docHelper ) ); + } + } + return fontList; + } + + private static boolean handleCell( Table table, DocCell docCell, boolean startHeader, DocTable docTable, OpenPdfHelper docHelper ) throws DocumentException, IOException { + OpenPpfDocHandler.setStyle( docTable, docCell ); + Cell cell = new Cell(); + if ( docCell.isHeader() ) { + cell.setHeader( true ); + startHeader = true; + } else { + if ( startHeader ) { + startHeader = false; + table.endHeaders(); + } + } + cell.setColspan( docCell.getCSpan() ); + cell.setRowspan( docCell.getRSpan() ); + + handleBolders(docCell, cell); + + if ( docCell.getBackColor() != null ) { + cell.setBackgroundColor( DocModelUtils.parseHtmlColor( docCell.getBackColor() ) ); + } + + handleAligns(docCell, cell); + + CellParent cellParent = new CellParent( cell ); + + List fontList = handleContent(table, cellParent, docCell, cell, docHelper); + + table.addCell( cell ); + List listChunk = cell.getChunks(); + if ( listChunk.size() == fontList.size() ) { + for ( int k=0; k itRow = docTable.docElements(); + while ( itRow.hasNext() ) { + DocRow docRow = (DocRow)itRow.next(); + Iterator itCell = docRow.docElements(); + while ( itCell.hasNext() ) { + DocCell docCell = (DocCell)itCell.next(); + startHeader = handleCell(table, docCell, startHeader, docTable, docHelper); + } + } + + return table; + } + + +} diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfFontHelper.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfFontHelper.java new file mode 100644 index 000000000..0744dd2a5 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfFontHelper.java @@ -0,0 +1,80 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import java.awt.Color; +import java.io.IOException; + +import org.fugerit.java.core.cfg.ConfigRuntimeException; +import org.fugerit.java.doc.base.model.DocPara; +import org.fugerit.java.doc.base.xml.DocModelUtils; + +import com.lowagie.text.DocumentException; +import com.lowagie.text.Font; +import com.lowagie.text.pdf.BaseFont; + +public class OpenPdfFontHelper { + + private OpenPdfFontHelper() {} + + public static BaseFont createBaseFontSafe( String name, String encoding, boolean embedded) { + BaseFont result = null; + try { + result = BaseFont.createFont(name, encoding, embedded); + } catch (DocumentException | IOException e) { + throw new ConfigRuntimeException( e ); + } + return result; + } + + private static int handleFontStyle( int style, int fontStyle ) { + if ( fontStyle == DocPara.STYLE_BOLD ) { + style = Font.BOLD; + } else if ( fontStyle == DocPara.STYLE_UNDERLINE ) { + style = Font.UNDERLINE; + } else if ( fontStyle == DocPara.STYLE_ITALIC ) { + style = Font.ITALIC; + } else if ( fontStyle == DocPara.STYLE_BOLDITALIC ) { + style = Font.BOLDITALIC; + } + return style; + } + + protected static Font createFont( String fontName, String fontPath, int fontSize, int fontStyle, OpenPdfHelper docHelper, String color ) throws DocumentException, IOException { + Font font = null; + int size = fontSize; + int style = Font.NORMAL; + BaseFont bf = null; + int bfV = -1; + if ( size == -1 ) { + size = Integer.parseInt( docHelper.getDefFontSize() ); + } + style = handleFontStyle(style, fontStyle); + if ( fontName == null ) { + fontName = docHelper.getDefFontName(); + } + if ( "helvetica".equalsIgnoreCase( fontName ) ) { + bfV = Font.HELVETICA; + } else if ( "courier".equalsIgnoreCase( fontName ) ) { + bfV = Font.COURIER; + } else if ( "times-roman".equalsIgnoreCase( fontName ) ) { + bfV = Font.TIMES_ROMAN; + } else if ( "symbol".equalsIgnoreCase( fontName ) ) { + bfV = Font.SYMBOL; + } else { + bf = OpenPpfDocHandler.findFont( fontName ); + if ( bf == null) { + bf = OpenPpfDocHandler.registerFont( fontName , fontPath ); + } + } + Color c = Color.BLACK; + if ( color != null ) { + c = DocModelUtils.parseHtmlColor( color ); + } + if ( bfV == -1 ) { + font = new Font( bf, size, style, c ); + } else { + font = new Font( bfV, size, style, c ); + } + return font; + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfHelper.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfHelper.java new file mode 100644 index 000000000..b45b625f5 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPdfHelper.java @@ -0,0 +1,63 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import java.util.Properties; + +import com.lowagie.text.pdf.PdfWriter; + +public class OpenPdfHelper { + + public OpenPdfHelper() { + this.params = new Properties(); + } + + private String defFontName; + + private String defFontSize; + + private String defFontStyle; + + private Properties params; + + public Properties getParams() { + return params; + } + + public void setParams(Properties params) { + this.params = params; + } + + public String getDefFontName() { + return defFontName; + } + + public void setDefFontName(String defFontName) { + this.defFontName = defFontName; + } + + public String getDefFontSize() { + return defFontSize; + } + + public void setDefFontSize(String defFontSize) { + this.defFontSize = defFontSize; + } + + public String getDefFontStyle() { + return defFontStyle; + } + + public void setDefFontStyle(String defFontStyle) { + this.defFontStyle = defFontStyle; + } + + private PdfWriter pdfWriter; + + public PdfWriter getPdfWriter() { + return pdfWriter; + } + + public void setPdfWriter(PdfWriter pdfWriter) { + this.pdfWriter = pdfWriter; + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPpfDocHandler.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPpfDocHandler.java new file mode 100644 index 000000000..908afdcea --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/OpenPpfDocHandler.java @@ -0,0 +1,447 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + + +import java.awt.Color; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; + +import org.fugerit.java.core.cfg.ConfigRuntimeException; +import org.fugerit.java.core.function.SafeFunction; +import org.fugerit.java.core.lang.helpers.StringUtils; +import org.fugerit.java.core.util.regex.ParamFinder; +import org.fugerit.java.doc.base.config.DocConfig; +import org.fugerit.java.doc.base.helper.SourceResolverHelper; +import org.fugerit.java.doc.base.model.DocBarcode; +import org.fugerit.java.doc.base.model.DocBase; +import org.fugerit.java.doc.base.model.DocElement; +import org.fugerit.java.doc.base.model.DocFooter; +import org.fugerit.java.doc.base.model.DocHeader; +import org.fugerit.java.doc.base.model.DocHeaderFooter; +import org.fugerit.java.doc.base.model.DocImage; +import org.fugerit.java.doc.base.model.DocInfo; +import org.fugerit.java.doc.base.model.DocPageBreak; +import org.fugerit.java.doc.base.model.DocPara; +import org.fugerit.java.doc.base.model.DocPhrase; +import org.fugerit.java.doc.base.model.DocStyle; +import org.fugerit.java.doc.base.model.DocTable; +import org.fugerit.java.doc.base.xml.DocModelUtils; + +import com.lowagie.text.Anchor; +import com.lowagie.text.BadElementException; +import com.lowagie.text.Chunk; +import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; +import com.lowagie.text.Element; +import com.lowagie.text.Font; +import com.lowagie.text.Header; +import com.lowagie.text.HeaderFooter; +import com.lowagie.text.Image; +import com.lowagie.text.Paragraph; +import com.lowagie.text.Phrase; +import com.lowagie.text.Rectangle; +import com.lowagie.text.html.HtmlTags; +import com.lowagie.text.pdf.Barcode; +import com.lowagie.text.pdf.Barcode128; +import com.lowagie.text.pdf.BarcodeEAN; +import com.lowagie.text.pdf.BaseFont; +import com.lowagie.text.pdf.PdfArray; +import com.lowagie.text.pdf.PdfDictionary; +import com.lowagie.text.pdf.PdfName; +import com.lowagie.text.pdf.PdfString; +import com.lowagie.text.pdf.PdfWriter; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class OpenPpfDocHandler { + + private static final ParamFinder PARAM_FINDER = ParamFinder.newFinder(); + + public static final String PARAM_PAGE_CURRENT = "currentPage"; + + public static final String PARAM_PAGE_TOTAL = "totalPage"; + + public static final String PARAM_PAGE_TOTAL_FINDER = ParamFinder.DEFAULT_PRE+PARAM_PAGE_TOTAL+ParamFinder.DEFAULT_POST; + + private static HashMap fonts = new HashMap<>(); + + public static BaseFont registerFont( String name, String path ) throws DocumentException, IOException { + BaseFont font = BaseFont.createFont( path, BaseFont.CP1252, true ); + registerFont( name, font ); + return font; + } + + public static void registerFont( String name, BaseFont font ) { + fonts.put( name , font ); + } + + public static BaseFont findFont( String name ) { + return fonts.get( name ); + } + + protected static void setStyle( DocStyle parent, DocStyle current ) { + if ( current.getBackColor() == null ) { + current.setBackColor( parent.getBackColor() ); + } + if ( current.getForeColor() == null ) { + current.setForeColor( parent.getForeColor() ); + } + } + + private PdfWriter pdfWriter; + + protected Document document; + + private String docType; + + public static final String MODULE = "openpdf-ext"; + + public static final String DOC_DEFAULT_FONT_NAME = "default-font-name"; + public static final String DOC_DEFAULT_FONT_SIZE = "default-font-size"; + public static final String DOC_DEFAULT_FONT_STYLE = "default-font-style"; + + private int totalPageCount; + + + public OpenPpfDocHandler( Document document, PdfWriter pdfWriter ) { + this(document, pdfWriter, -1); + } + + public OpenPpfDocHandler( Document document, PdfWriter pdfWriter, int totalPageCount ) { + this( document, DocConfig.TYPE_PDF ); + this.pdfWriter = pdfWriter; + this.totalPageCount = totalPageCount; + } + + public OpenPpfDocHandler( Document document, String docType ) { + this.document = document; + this.docType = docType; + } + + private static int getAlign( int align ) { + int r = Element.ALIGN_LEFT; + if ( align == DocPara.ALIGN_RIGHT ) { + r = Element.ALIGN_RIGHT; + } else if ( align == DocPara.ALIGN_CENTER ) { + r = Element.ALIGN_CENTER; + } else if ( align == DocPara.ALIGN_JUSTIFY ) { + r = Element.ALIGN_JUSTIFIED; + } else if ( align == DocPara.ALIGN_JUSTIFY_ALL ) { + r = Element.ALIGN_JUSTIFIED_ALL; + } + return r; + } + + + protected static Image createImage( DocImage docImage ) { + String url = docImage.getUrl(); + log.trace( "currently unsupported image param url {}", url ); + return SafeFunction.get( () -> { + byte[] data = SourceResolverHelper.resolveImage( docImage ); + Image image = Image.getInstance( data ); + if ( docImage.getScaling() != null ) { + image.scalePercent( docImage.getScaling().floatValue() ); + } + return image; + } ); + } + + + public static String createText( Properties params, String text ) { + return PARAM_FINDER.substitute( text , params ); + } + + protected static Chunk createChunk( DocPhrase docPhrase, OpenPdfHelper docHelper ) throws DocumentException, IOException { + String text = createText( docHelper.getParams(), docPhrase.getText() ); + int style = docPhrase.getStyle(); + String fontName = docPhrase.getFontName(); + Font f = createFont(fontName, docPhrase.getSize(), style, docHelper, docPhrase.getForeColor() ); + return new Chunk( text, f ); + } + + protected static Phrase createPhrase( DocPhrase docPhrase, OpenPdfHelper docHelper, List fontMap ) throws DocumentException, IOException { + String text = createText( docHelper.getParams(), docPhrase.getText() ); + int style = docPhrase.getStyle(); + String fontName = docPhrase.getFontName(); + Font f = createFont(fontName, docPhrase.getSize(), style, docHelper, docPhrase.getForeColor() ); + Phrase p = null; + if ( StringUtils.isNotEmpty( docPhrase.getLink() ) ) { + Anchor a = new Anchor( text , f ); + a.setReference( docPhrase.getLink() ); + p = a; + } else if ( StringUtils.isNotEmpty( docPhrase.getAnchor() ) ) { + Anchor a = new Anchor( text , f ); + a.setName( docPhrase.getAnchor() ); + p = a; + } else { + p = new Phrase( text, f ); + } + if (fontMap != null) { + fontMap.add( f ); + } + return p; + } + + protected static Phrase createPhrase( DocPhrase docPhrase, OpenPdfHelper docHelper ) throws DocumentException, IOException { + return createPhrase(docPhrase, docHelper, null); + } + + protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper ) throws DocumentException, IOException { + return createPara(docPara, docHelper, null); + } + + protected static Paragraph createPara( DocPara docPara, OpenPdfHelper docHelper, List fontMap ) throws DocumentException, IOException { + int style = docPara.getStyle(); + String text = createText( docHelper.getParams(), docPara.getText() ); + String fontName = docPara.getFontName(); + Font f = createFont(fontName, docPara.getSize(), style, docHelper, docPara.getForeColor() ); + Phrase phrase = new Phrase( text, f ); + Paragraph p = new Paragraph( new Phrase( text, f ) ); + if ( docPara.getForeColor() != null ) { + Color c = DocModelUtils.parseHtmlColor( docPara.getForeColor() ); + Font f1 = new Font( f.getFamily(), f.getSize(), f.getStyle(), c ); + p = new Paragraph( new Phrase( text, f1 ) ); + } + if ( docPara.getAlign() != DocPara.ALIGN_UNSET ) { + p.setAlignment( getAlign( docPara.getAlign() ) ); + } + if ( docPara.getLeading() != null ) { + p.setLeading( docPara.getLeading().floatValue() ); + } + if ( docPara.getSpaceBefore() != null ) { + p.setSpacingBefore( docPara.getSpaceBefore().floatValue() ); + } + if ( docPara.getSpaceAfter() != null ) { + p.setSpacingAfter( docPara.getSpaceAfter().floatValue() ); + } + p.setFont( f ); + phrase.setFont( f ); + if ( fontMap != null ) { + fontMap.add( f ); + } + return p; + } + + protected static Image createBarcode( DocBarcode docBarcode, OpenPdfHelper helper ) throws BadElementException, IOException { + log.trace( "currently unused parameter helper : {}", helper ); + Barcode barcode = null; + if ( "128".equalsIgnoreCase( docBarcode.getType() ) ) { + barcode = new Barcode128(); + } else { + barcode = new BarcodeEAN(); + } + if ( docBarcode.getSize() != -1 ) { + barcode.setBarHeight( docBarcode.getSize() ); + } + barcode.setCode( docBarcode.getText() ); + barcode.setAltText( docBarcode.getText() ); + java.awt.Image awtImage = barcode.createAwtImage( Color.white, Color.black ); + return Image.getInstance( awtImage, null ); + } + + + + public static Font createFont( String fontName, int fontSize, int fontStyle, OpenPdfHelper docHelper, String color ) throws DocumentException, IOException { + return OpenPdfFontHelper.createFont(fontName, fontName, fontSize, fontStyle, docHelper, color); + } + + private void handleTypeSpecific( Properties info ) { + // per documenti tipo HTML + if ( DocConfig.TYPE_HTML.equalsIgnoreCase( this.docType ) ) { + String cssLink = info.getProperty( DocInfo.INFO_NAME_CSS_LINK ); + if ( cssLink != null ) { + this.document.add( new Header( HtmlTags.STYLESHEET, cssLink ) ); + } + } + // per documenti tipo word o pdf + if ( DocConfig.TYPE_PDF.equalsIgnoreCase( this.docType ) || DocConfig.TYPE_RTF.equalsIgnoreCase( this.docType ) ) { + Rectangle size = this.document.getPageSize(); + String pageOrient = info.getProperty( DocInfo.INFO_NAME_PAGE_ORIENT ); + if ( "horizontal".equalsIgnoreCase( pageOrient ) ) { + size = new Rectangle( size.getHeight(), size.getWidth() ); + this.document.setPageSize( size ); + } + if ( DocConfig.TYPE_PDF.equalsIgnoreCase( this.docType ) ) { + String pdfFormat = info.getProperty( DocInfo.INFO_NAME_PDF_FORMAT ); + if ( DocConfig.FORMAT_PDF_A_1B.equalsIgnoreCase( pdfFormat ) ) { + this.pdfWriter.setPDFXConformance(PdfWriter.PDFA1B); + PdfDictionary outi = new PdfDictionary( PdfName.OUTPUTINTENT ); + outi.put( PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1") ); + outi.put( PdfName.INFO, new PdfString("sRGB IEC61966-2.1") ); + outi.put( PdfName.S, PdfName.GTS_PDFA1 ); + this.pdfWriter.getExtraCatalog().put( PdfName.OUTPUTINTENTS, new PdfArray( outi ) ); + } + + } + } + } + + protected void handleHeaderExt( DocHeader docHeader, PdfHelper pdfHelper, OpenPdfHelper docHelper ) throws DocumentException { + log.trace( "docHelper : {}", docHelper ); + pdfHelper.setDocHeader( docHeader ); + } + + protected void handleFooterExt( DocFooter docFooter, PdfHelper pdfHelper, OpenPdfHelper docHelper ) throws DocumentException { + log.trace( "docHelper : {}", docHelper ); + pdfHelper.setDocFooter( docFooter ); + } + + private void handleHeader( DocBase docBase, PdfHelper pdfHelper, OpenPdfHelper docHelper) throws DocumentException, IOException { + DocHeader docHeader = docBase.getDocHeader(); + if ( docHeader != null && docHeader.isUseHeader() ) { + if ( docHeader.isBasic() ) { + HeaderFooter header = this.createHeaderFooter( docHeader, docHeader.getAlign(), docHelper ); + this.document.setHeader( header ); + } else { + this.handleHeaderExt(docHeader, pdfHelper, docHelper); + } + } + } + + private void handleFooter( DocBase docBase, PdfHelper pdfHelper, OpenPdfHelper docHelper) throws DocumentException, IOException { + DocFooter docFooter = docBase.getDocFooter(); + if ( docFooter != null && docFooter.isUseFooter() ) { + if ( docFooter.isBasic() ) { + HeaderFooter footer = this.createHeaderFooter( docFooter, docFooter.getAlign(), docHelper ); + this.document.setFooter( footer ); + } else { + this.handleFooterExt(docFooter, pdfHelper, docHelper); + } + } + } + + /* (non-Javadoc) + * @see org.fugerit.java.doc.base.DocHandler#handleDoc(org.fugerit.java.doc.base.DocBase) + */ + public void handleDoc(DocBase docBase) throws DocumentException, IOException { + Properties info = docBase.getInfo(); + + String defaultFontName = info.getProperty( DOC_DEFAULT_FONT_NAME, "helvetica" ); + String defaultFontSize = info.getProperty( DOC_DEFAULT_FONT_SIZE, "10" ); + String defaultFontStyle = info.getProperty( DOC_DEFAULT_FONT_STYLE, "normal" ); + OpenPdfHelper docHelper = new OpenPdfHelper(); + + if ( this.pdfWriter != null ) { + docHelper.setPdfWriter( this.pdfWriter ); + } + + docHelper.setDefFontName( defaultFontName ); + docHelper.setDefFontStyle( defaultFontStyle ); + docHelper.setDefFontSize( defaultFontSize ); + + if ( this.totalPageCount != -1 ) { + docHelper.getParams().setProperty( PARAM_PAGE_TOTAL , String.valueOf( this.totalPageCount ) ); + } + + this.handleTypeSpecific(info); + + // header / footer section + PdfHelper pdfHelper = new PdfHelper( docHelper ); + this.handleHeader(docBase, pdfHelper, docHelper); + this.handleFooter(docBase, pdfHelper, docHelper); + if ( DocConfig.TYPE_PDF.equals( this.docType ) ) { + this.pdfWriter.setPageEvent( pdfHelper ); + } + + this.document.open(); + + Iterator itDoc = docBase.getDocBody().docElements(); + handleElements(document, itDoc, docHelper); + + this.document.close(); + } + + public static void handleElementsSafe( Document document, Iterator itDoc, OpenPdfHelper docHelper ) { + try { + handleElements(document, itDoc, docHelper); + } catch (Exception e) { + throw new ConfigRuntimeException( e ); + } + } + + public static void handleElements( Document document, Iterator itDoc, OpenPdfHelper docHelper ) throws DocumentException, IOException { + while ( itDoc.hasNext() ) { + DocElement docElement = itDoc.next(); + getElement(document, docElement, true, docHelper ); + } + } + + public static Element getElement( Document document, DocElement docElement, boolean addElement, OpenPdfHelper docHelper ) throws DocumentException, IOException { + Element result = null; + DocumentParent documentParent = new DocumentParent( document ); + if ( docElement instanceof DocPhrase ) { + result = createPhrase( (DocPhrase)docElement, docHelper ); + if ( addElement ) { + documentParent.add( result ); + } + } else if ( docElement instanceof DocPara ) { + result = createPara( (DocPara)docElement, docHelper ); + if ( addElement ) { + documentParent.add( result ); + } + } else if ( docElement instanceof DocTable ) { + result = OpenPdfDocTableHelper.createTable( (DocTable)docElement, docHelper ); + if ( addElement ) { + document.add( result ); + } + } else if ( docElement instanceof DocImage ) { + result = createImage( (DocImage)docElement ); + if ( addElement ) { + documentParent.add( result ); + } + } else if ( docElement instanceof DocPageBreak ) { + document.newPage(); + } + return result; + } + + private void handleHeaderFooterElement( DocElement docElement, float leading, OpenPdfHelper docHelper , Phrase phrase ) throws DocumentException, IOException { + if ( docElement instanceof DocPhrase ) { + DocPhrase docPhrase = (DocPhrase) docElement; + Chunk ck = createChunk( docPhrase, docHelper ); + if( docPhrase.getLeading() != null && docPhrase.getLeading().floatValue() != leading ) { + leading = docPhrase.getLeading().floatValue(); + phrase.setLeading( leading ); + } + phrase.add( ck ); + } else if ( docElement instanceof DocPara ) { + DocPara docPara = (DocPara) docElement; + if ( docPara.getLeading() != null ) { + phrase.setLeading( docPara.getLeading().floatValue() ); + } + Font f = new Font( Font.HELVETICA, docPara.getSize() ); + if ( docPara.getForeColor() != null ) { + SafeFunction.applySilent( () -> f.setColor( DocModelUtils.parseHtmlColor( docPara.getForeColor() ) ) ); + } + Chunk ck = new Chunk( docPara.getText(), f ); + phrase.add( ck ); + } else if ( docElement instanceof DocImage ) { + DocImage docImage = (DocImage)docElement; + Image img = createImage( docImage ); + Chunk ck = new Chunk( img, 0, 0, true ); + phrase.add( ck ); + } + } + + private HeaderFooter createHeaderFooter( DocHeaderFooter container, int align, OpenPdfHelper docHelper ) throws DocumentException, IOException { + Iterator it = container.docElements(); + Phrase phrase = new Phrase(); + float leading = (float)-1.0; + while ( it.hasNext() ) { + DocElement docElement = it.next(); + this.handleHeaderFooterElement(docElement, leading, docHelper, phrase); + } + HeaderFooter headerFooter = new HeaderFooter( phrase, container.isNumbered() ); + if ( align == DocPara.ALIGN_UNSET ) { + align = DocPara.ALIGN_CENTER; + } + headerFooter.setAlignment( getAlign( align ) ); + headerFooter.setBorder( container.getBorderWidth() ); + return headerFooter; + } + +} + diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/ParentElement.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/ParentElement.java new file mode 100644 index 000000000..37075a867 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/ParentElement.java @@ -0,0 +1,11 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import org.fugerit.java.doc.base.config.DocException; + +import com.lowagie.text.Element; + +public interface ParentElement { + + public void add( Element element ) throws DocException; + +} diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/PdfHelper.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/PdfHelper.java new file mode 100644 index 000000000..3445e7eba --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/PdfHelper.java @@ -0,0 +1,143 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import java.util.Iterator; + +import org.fugerit.java.core.cfg.ConfigRuntimeException; +import org.fugerit.java.doc.base.model.DocElement; +import org.fugerit.java.doc.base.model.DocFooter; +import org.fugerit.java.doc.base.model.DocHeader; +import org.fugerit.java.doc.base.model.DocPara; + +import com.lowagie.text.Document; +import com.lowagie.text.Element; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.BaseFont; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfPageEventHelper; +import com.lowagie.text.pdf.PdfTemplate; +import com.lowagie.text.pdf.PdfWriter; + +/** + * This is the handler to use + * + * @author fugerit79 + * + */ +public class PdfHelper extends PdfPageEventHelper { + + public PdfHelper( OpenPdfHelper docHelper ) { + this.docHelper = docHelper; + this.docHeader = null; + this.docFooter = null; + } + + protected BaseFont baseFont; + private PdfTemplate totalPages; + private float footerTextSize = 8f; + private int pageNumberAlignment = Element.ALIGN_CENTER; + + private OpenPdfHelper docHelper; + + private int currentPageNumber; + + private DocHeader docHeader; + + private DocFooter docFooter; + + @Override + public void onStartPage(PdfWriter writer, Document document) { + this.currentPageNumber = writer.getPageNumber(); + this.docHelper.getParams().setProperty( OpenPpfDocHandler.PARAM_PAGE_CURRENT , String.valueOf( writer.getPageNumber() ) ); + if ( this.getDocHeader() != null ) { + OpenPpfDocHandler.handleElementsSafe( document, this.getDocHeader().docElements(), docHelper ); + } + } + + @Override + public void onOpenDocument(PdfWriter writer, Document document) { + totalPages = writer.getDirectContent().createTemplate(100, 100); + totalPages.setBoundingBox( new Rectangle(-20, -20, 100, 100) ); + this.baseFont = OpenPdfFontHelper.createBaseFontSafe(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); + } + + @Override + public void onEndPage(PdfWriter writer, Document document) { + if ( this.getDocFooter() != null && !this.getDocFooter().isBasic() ) { + // allocate direct writer + PdfContentByte cb = writer.getDirectContent(); + // save writer state + cb.saveState(); + Iterator itElements = this.getDocFooter().docElements(); + int totalOffset = 20; + cb.beginText(); + cb.setFontAndSize(baseFont, footerTextSize); + while ( itElements.hasNext() ) { + DocElement current = itElements.next(); + if ( current instanceof DocPara ) { + DocPara para = (DocPara) current; + String text = OpenPpfDocHandler.createText( docHelper.getParams(), para.getText() ); + float textSize = baseFont.getWidthPoint(text, footerTextSize); + float textBase = document.bottom() - totalOffset; + int rowOffset = 10; + if( para.getAlign() == DocPara.ALIGN_CENTER ) { + cb.setTextMatrix((document.right() / 2), textBase); + cb.showText(text); + } else if( para.getAlign() == DocPara.ALIGN_LEFT ) { + cb.setTextMatrix(document.left(), textBase); + cb.showText(text); + } else { + float adjust = baseFont.getWidthPoint("0", footerTextSize); + cb.setTextMatrix(document.right() - textSize - adjust, textBase); + cb.showText(text); + } + + totalOffset+= rowOffset; + } else { + throw new ConfigRuntimeException( "Element not allowed in footer (accepted only DocPara) : "+current ); + } + + } + cb.endText(); + // restore writer state + cb.restoreState(); + } + } + + @Override + public void onCloseDocument(PdfWriter writer, Document document) { + totalPages.beginText(); + totalPages.setFontAndSize(baseFont, footerTextSize); + totalPages.setTextMatrix(0, 0); + totalPages.showText(String.valueOf( writer.getPageNumber() - 1) ); + totalPages.endText(); + } + + public void setPageNumberAlignment(int pageNumberAlignment) { + this.pageNumberAlignment = pageNumberAlignment; + } + + public DocHeader getDocHeader() { + return docHeader; + } + + public void setDocHeader(DocHeader docHeader) { + this.docHeader = docHeader; + } + + public DocFooter getDocFooter() { + return docFooter; + } + + public void setDocFooter(DocFooter docFooter) { + this.docFooter = docFooter; + } + + public int getPageNumberAlignment() { + return pageNumberAlignment; + } + + public int getCurrentPageNumber() { + return currentPageNumber; + } + +} \ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/PhraseParent.java b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/PhraseParent.java new file mode 100644 index 000000000..6aa5f2505 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/main/java/org/fugerit/java/doc/mod/openpdf/ext/helpers/PhraseParent.java @@ -0,0 +1,23 @@ +package org.fugerit.java.doc.mod.openpdf.ext.helpers; + +import org.fugerit.java.doc.base.config.DocException; + +import com.lowagie.text.Element; +import com.lowagie.text.Phrase; + +public class PhraseParent implements ParentElement { + + private Phrase phrase; + + public PhraseParent( Phrase phrase ) { + this.phrase = phrase; + } + + /* (non-Javadoc) + * @see org.fugerit.java.doc.mod.itext.ParentElement#add(com.lowagie.text.Element) + */ + public void add(Element element) throws DocException { + this.phrase.add( element ); + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestDefaultDoc.java b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestDefaultDoc.java new file mode 100644 index 000000000..7b36f597a --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestDefaultDoc.java @@ -0,0 +1,69 @@ +package test.org.fugerit.java.doc.mod.openpdf.ext; + +import org.fugerit.java.doc.base.config.DocConfig; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPpfDocHandler; +import org.junit.Assert; +import org.junit.Test; + +import com.lowagie.text.pdf.BaseFont; + +public class TestDefaultDoc extends TestDocBase { + + private static final String CUSTOM_FONT = "TitilliumWeb"; + + private static final String DEFAULT_DOC = "default_doc"; + + private static final String DEFAULT_DOC_ALT = "default_doc_alt"; + + private static final String DEFAULT_DOC_SIMPLE = "default_doc_simple"; + + private static final String DEFAULT_DOC_PDFA = "default_doc_pdfa"; + + @Test + public void testOpenFailPDF() { + Assert.assertThrows( AssertionError.class , () -> this.testDocWorker( "default_doc_fail1" , DocConfig.TYPE_PDF ) ); + } + + @Test + public void testCustomFont() { + BaseFont font = OpenPpfDocHandler.findFont( CUSTOM_FONT ); + Assert.assertNotNull(font); + } + + @Test + public void testOpenPDF() { + boolean ok = this.testDocWorker( DEFAULT_DOC , DocConfig.TYPE_PDF ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenHTML() { + boolean ok = this.testDocWorker( DEFAULT_DOC , DocConfig.TYPE_HTML ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenAltPDF() { + boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , DocConfig.TYPE_PDF ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenAltHTML() { + boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , DocConfig.TYPE_HTML ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenSimple() { + boolean ok = this.testDocWorker( DEFAULT_DOC_SIMPLE , DocConfig.TYPE_PDF ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenPDFA() { + // still working on font embedding + Assert.assertThrows( AssertionError.class , () -> this.testDocWorker( DEFAULT_DOC_PDFA , DocConfig.TYPE_PDF )); + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestDocBase.java b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestDocBase.java new file mode 100644 index 000000000..73397eb60 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestDocBase.java @@ -0,0 +1,42 @@ +package test.org.fugerit.java.doc.mod.openpdf.ext; + +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; + +import org.fugerit.java.core.lang.helpers.ClassHelper; +import org.fugerit.java.doc.base.config.DocInput; +import org.fugerit.java.doc.base.config.DocOutput; +import org.fugerit.java.doc.base.config.DocTypeHandler; +import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfig; +import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfigFacade; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TestDocBase { + + private FreemarkerDocProcessConfig config = FreemarkerDocProcessConfigFacade.loadConfigSafe( "cl://fm-test-mod-openpdf-config.xml" ); + + protected boolean testDocWorker( String testCase, String type ) { + boolean ok = false; + String inputXml = "xml/"+testCase+".xml" ; + DocTypeHandler handler = this.config.getFacade().findHandler( type ); + File outputFile = new File( "target", testCase+"."+handler.getType() ); + log.info( "inputXml:{}, outputFile:{}", inputXml, outputFile ); + try ( InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( inputXml ) ); + OutputStream os = new FileOutputStream( outputFile ) ) { + handler.handle( DocInput.newInput( handler.getType() , reader ) , DocOutput.newOutput(os) ); + ok = true; + } catch (Exception e) { + String message = "Error : "+e.getMessage(); + log.error( message , e ); + fail( message ); + } + return ok; + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestOpenPdfHelper.java b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestOpenPdfHelper.java new file mode 100644 index 000000000..01c17308f --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestOpenPdfHelper.java @@ -0,0 +1,19 @@ +package test.org.fugerit.java.doc.mod.openpdf.ext; + +import org.fugerit.java.doc.mod.openpdf.ext.helpers.PhraseParent; +import org.junit.Assert; +import org.junit.Test; + +import com.lowagie.text.Paragraph; +import com.lowagie.text.Phrase; + +public class TestOpenPdfHelper { + + @Test + public void test001() throws Exception { + PhraseParent parent = new PhraseParent( new Phrase() ); + parent.add( new Paragraph() ); + Assert.assertNotNull( parent ); + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestPdfHelper.java b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestPdfHelper.java new file mode 100644 index 000000000..a09918f73 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestPdfHelper.java @@ -0,0 +1,40 @@ +package test.org.fugerit.java.doc.mod.openpdf.ext; + +import org.fugerit.java.core.cfg.ConfigRuntimeException; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPdfFontHelper; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPpfDocHandler; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.PdfHelper; +import org.junit.Assert; +import org.junit.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TestPdfHelper { + + @Test + public void testSetGet() { + PdfHelper helper = new PdfHelper( null ); + log.info( "getCurrentPageNumber -> {}", helper.getCurrentPageNumber() ); + int current = 10; + helper.setPageNumberAlignment(current); + log.info( "getPageNumberAlignment -> {}", helper.getPageNumberAlignment() ); + Assert.assertEquals( current , helper.getPageNumberAlignment() ); + } + + + @Test + public void testTandleElementSafe() { + Assert.assertThrows( ConfigRuntimeException.class , () -> OpenPpfDocHandler.handleElementsSafe(null, null, null) ); + } + + @Test + public void testCreateFontSafe() { + Assert.assertThrows( RuntimeException.class , () -> OpenPdfFontHelper.createBaseFontSafe(null, null, false) ); + } + + @Test + public void testCreateFontSafe1() { + Assert.assertThrows( ConfigRuntimeException.class , () -> OpenPdfFontHelper.createBaseFontSafe( "no", "no", false) ); + } +} diff --git a/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestPhraseParent.java b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestPhraseParent.java new file mode 100644 index 000000000..54ba74c9e --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestPhraseParent.java @@ -0,0 +1,22 @@ +package test.org.fugerit.java.doc.mod.openpdf.ext; + +import java.util.Properties; + +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPdfHelper; +import org.junit.Assert; +import org.junit.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TestPhraseParent { + + @Test + public void test001() throws Exception { + OpenPdfHelper helper = new OpenPdfHelper(); + helper.setParams( new Properties() ); + log.info( "helper.getDefFontStyle() -> {}", helper.getDefFontStyle() ); + Assert.assertNotNull( helper.getParams() ); + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestSpec.java b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestSpec.java new file mode 100644 index 000000000..8fc8ed2bc --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/java/test/org/fugerit/java/doc/mod/openpdf/ext/TestSpec.java @@ -0,0 +1,17 @@ +package test.org.fugerit.java.doc.mod.openpdf.ext; + +import org.fugerit.java.doc.base.config.DocConfig; +import org.junit.Assert; +import org.junit.Test; + +public class TestSpec extends TestDocBase { + + private static final String DEFAULT_DOC = "test"; + + @Test + public void testOpenPDF() { + boolean ok = this.testDocWorker( DEFAULT_DOC , DocConfig.TYPE_PDF ); + Assert.assertTrue(ok); + } + +} diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/fm-test-mod-openpdf-config.xml b/fj-doc-mod-openpdf-ext/src/test/resources/fm-test-mod-openpdf-config.xml new file mode 100644 index 000000000..708a173a1 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/fm-test-mod-openpdf-config.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Bold.ttf b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Bold.ttf new file mode 100644 index 000000000..b51a4d638 Binary files /dev/null and b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Bold.ttf differ diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-BoldItalic.ttf b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-BoldItalic.ttf new file mode 100644 index 000000000..d0fe23bed Binary files /dev/null and b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-BoldItalic.ttf differ diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Italic.ttf b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Italic.ttf new file mode 100644 index 000000000..266d5cb81 Binary files /dev/null and b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Italic.ttf differ diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Regular.ttf b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Regular.ttf new file mode 100644 index 000000000..a54ad4b93 Binary files /dev/null and b/fj-doc-mod-openpdf-ext/src/test/resources/font/TitilliumWeb-Regular.ttf differ diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/log4j2.xml b/fj-doc-mod-openpdf-ext/src/test/resources/log4j2.xml new file mode 100644 index 000000000..51b9129fc --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/test/img_test_teal.jpg b/fj-doc-mod-openpdf-ext/src/test/resources/test/img_test_teal.jpg new file mode 100644 index 000000000..a8213e159 Binary files /dev/null and b/fj-doc-mod-openpdf-ext/src/test/resources/test/img_test_teal.jpg differ diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc.xml b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc.xml new file mode 100644 index 000000000..7e293e90b --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc.xml @@ -0,0 +1,59 @@ + + + + + + 10;10;10;30 + excel-table=print + + Basic example + fj doc venus sample source xml + fugerit79 + en + + true + /css/test.css + + header test + + + test + ${r"${currentPage}"} / ${r"${pageCount}"} + test + + + + + Test times roman + Courier + Symbol + Symbol + Test default font + + + Name + Surname + Title + + + + + + + + + + + + + + + + +
+ + +
\ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_alt.xml b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_alt.xml new file mode 100644 index 000000000..21765c2a1 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_alt.xml @@ -0,0 +1,67 @@ + + + + + + 10;10;10;30 + excel-table=print + + Basic example + fj doc venus sample source xml + fugerit79 + en + + horizontal +
+ header test +
+
+ ${r"${currentPage}"} / ${r"${pageCount}"} +
+
+ + + My sample title + + + Name + Surname + Title + + + + + + + + + + + + + + + +
+ + 1 + 2 + +
+ + + + + + + + + + + + + +
\ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_fail1.xml b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_fail1.xml new file mode 100644 index 000000000..f8335b706 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_fail1.xml @@ -0,0 +1,58 @@ + + + + + + 10;10;10;30 + excel-table=print + + Basic example + fj doc venus sample source xml + fugerit79 + en + + true + /css/test.css + + header test + + + test + ${r"${currentPage}"} / ${r"${pageCount}"} + + + + + Test times roman + Courier + Symbol + Symbol + Test default font + + + Name + Surname + Title + + + + + + + + + + + + + + + + +
+ + +
\ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_pdfa.xml b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_pdfa.xml new file mode 100644 index 000000000..06d6f5f60 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_pdfa.xml @@ -0,0 +1,16 @@ + + + + + + TitilliumWeb + PDF/A-1b + + + Test PDFA + + + \ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_simple.xml b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_simple.xml new file mode 100644 index 000000000..6e058f19b --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/xml/default_doc_simple.xml @@ -0,0 +1,17 @@ + + + + + TitilliumWeb +
+ +
+
+ + Test PDFA + + +
\ No newline at end of file diff --git a/fj-doc-mod-openpdf-ext/src/test/resources/xml/test.xml b/fj-doc-mod-openpdf-ext/src/test/resources/xml/test.xml new file mode 100644 index 000000000..f8d34b0a1 --- /dev/null +++ b/fj-doc-mod-openpdf-ext/src/test/resources/xml/test.xml @@ -0,0 +1,44 @@ + + + + + + TitilliumWeb + 10 + 50;50;20;20 + 1 + Test doc + Fugerit + Venus + en + + + + + + + + + + + +
+
+ + + ${currentPage} / ${pageCount} + + + + Bookmark 1 + + +
+ + MAIN TITLE + Secondary title + +
\ No newline at end of file diff --git a/fj-doc-mod-openrtf-ext/README.md b/fj-doc-mod-openrtf-ext/README.md new file mode 100644 index 000000000..4b72a7d1d --- /dev/null +++ b/fj-doc-mod-openrtf-ext/README.md @@ -0,0 +1,31 @@ +# Fugerit Document Generation Framework (fj-doc) + +## Openrtf Renderer (rtf)(fj-doc-mod-openrtf-ext) + +[back to fj-doc index](../README.md) + +[![Maven Central](https://img.shields.io/maven-central/v/org.fugerit.java/fj-doc-mod-openrtf-ext.svg)](https://mvnrepository.com/artifact/org.fugerit.java/fj-doc-mod-openrtf-ext) +[![javadoc](https://javadoc.io/badge2/org.fugerit.java/fj-doc-mod-openrtf-ext/javadoc.svg)](https://javadoc.io/doc/org.fugerit.java/fj-doc-mod-openrtf-ext) + +Renderer for rtf format, based on [OpenRTF](https://github.com/Librertf/OpenRTF). + +*Status* : +Basic features implemented. (Sample JUnit [TestFreeMarker01](../fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/TestFreeMarker01.java) is now working). + +*Since* : fj-doc 3.4 + +*Native support* : +Disabled, native support will be added in a future release. openrtf-ext native support must be verified. + +*Quickstart* : +Basically this is only a type handler, see core library [fj-doc-base](../fj-doc-base/README.md). +NOTE: If you have any special need you can open a pull request or create your own handler based on this. + +Depends on renderer : + +``` + + org.fugerit.java + fj-doc-mod-openpdf-ext + +``` diff --git a/fj-doc-mod-openrtf-ext/pom.xml b/fj-doc-mod-openrtf-ext/pom.xml new file mode 100644 index 000000000..bbc405707 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/pom.xml @@ -0,0 +1,66 @@ + + 4.0.0 + + fj-doc-mod-openrtf-ext + + + org.fugerit.java + fj-doc + 3.3.2-SNAPSHOT + + + fj-doc-mod-openrtf-ext + Renderer for RTF format, based on https://github.com/LibrePDF/OpenRTF + + + 1.2.1 + + + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + + com.github.librepdf + openrtf + ${openrtf-version-version} + + + + org.fugerit.java + fj-core + + + + org.fugerit.java + fj-doc-base + + + + org.fugerit.java + fj-doc-mod-openpdf-ext + + + + org.fugerit.java + fj-doc-freemarker + test + + + + + + https://www.fugerit.org + Fugerit + + + https://www.fugerit.org/perm/venus/ + + diff --git a/fj-doc-mod-openrtf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/RtfTypeHandler.java b/fj-doc-mod-openrtf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/RtfTypeHandler.java new file mode 100644 index 000000000..e527e8fb8 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/RtfTypeHandler.java @@ -0,0 +1,59 @@ +package org.fugerit.java.doc.mod.openrtf.ext; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +import org.fugerit.java.core.cfg.ConfigException; +import org.fugerit.java.doc.base.config.DocConfig; +import org.fugerit.java.doc.base.config.DocInput; +import org.fugerit.java.doc.base.config.DocOutput; +import org.fugerit.java.doc.base.config.DocTypeHandler; +import org.fugerit.java.doc.base.config.DocTypeHandlerDefault; +import org.fugerit.java.doc.base.model.DocBase; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPDFConfigHelper; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPpfDocHandler; +import org.fugerit.java.doc.mod.openrtf.ext.helpers.OpenRtfDocHandler; +import org.w3c.dom.Element; + +import com.lowagie.text.Document; +import com.lowagie.text.PageSize; +import com.lowagie.text.rtf.RtfWriter2; + +public class RtfTypeHandler extends DocTypeHandlerDefault { + + /** + * + */ + private static final long serialVersionUID = 7532380038613171305L; + + public static final DocTypeHandler HANDLER = new RtfTypeHandler(); + + public RtfTypeHandler() { + super( DocConfig.TYPE_RTF, OpenRtfDocHandler.MODULE ); + } + + @Override + public void handle(DocInput docInput, DocOutput docOutput) throws Exception { + DocBase docBase = docInput.getDoc(); + OutputStream outputStream = docOutput.getOs(); + String[] margins = docBase.getInfo().getProperty( "margins", "20;20;20;20" ).split( ";" ); + Document document = new Document( PageSize.A4, Integer.parseInt( margins[0] ), + Integer.parseInt( margins[1] ), + Integer.parseInt( margins[2] ), + Integer.parseInt( margins[3] ) ); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + RtfWriter2 rtfWriter2 = RtfWriter2.getInstance( document, baos ); + OpenPpfDocHandler handler = new OpenRtfDocHandler( document, rtfWriter2 ); + handler.handleDoc( docBase ); + baos.writeTo( outputStream ); + baos.close(); + outputStream.close(); + } + + @Override + protected void handleConfigTag(Element config) throws ConfigException { + super.handleConfigTag(config); + OpenPDFConfigHelper.handleConfig( config, this.getType() ); + } + +} diff --git a/fj-doc-mod-openrtf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/helpers/OpenRtfDocHandler.java b/fj-doc-mod-openrtf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/helpers/OpenRtfDocHandler.java new file mode 100644 index 000000000..37a20f985 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/main/java/org/fugerit/java/doc/mod/openrtf/ext/helpers/OpenRtfDocHandler.java @@ -0,0 +1,67 @@ +package org.fugerit.java.doc.mod.openrtf.ext.helpers; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.fugerit.java.core.function.SafeFunction; +import org.fugerit.java.doc.base.config.DocConfig; +import org.fugerit.java.doc.base.model.DocElement; +import org.fugerit.java.doc.base.model.DocFooter; +import org.fugerit.java.doc.base.model.DocHeader; +import org.fugerit.java.doc.base.model.DocHeaderFooter; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPdfHelper; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.OpenPpfDocHandler; +import org.fugerit.java.doc.mod.openpdf.ext.helpers.PdfHelper; + +import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; +import com.lowagie.text.Element; +import com.lowagie.text.rtf.RtfWriter2; +import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class OpenRtfDocHandler extends OpenPpfDocHandler { + + public static final String MODULE = "openrtf-ext"; + + public OpenRtfDocHandler( Document document, RtfWriter2 rtfWriter2 ) { + super( document, DocConfig.TYPE_RTF ); + log.trace( "currently unused parameter rtfWriter2 {}", rtfWriter2 ); + } + + @Override + protected void handleHeaderExt( DocHeader docHeader, PdfHelper pdfHelper, OpenPdfHelper docHelper ) throws DocumentException { + this.document.setHeader( new RtfHeaderFooter( createRtfHeaderFooter( docHeader , this.document, true, docHelper ) ) ); + } + + @Override + protected void handleFooterExt( DocFooter docFooter, PdfHelper pdfHelper, OpenPdfHelper docHelper ) throws DocumentException { + this.document.setFooter( new RtfHeaderFooter( createRtfHeaderFooter( docFooter , this.document, false, docHelper ) ) ); + } + + private static RtfHeaderFooter createRtfHeaderFooter( DocHeaderFooter docHeaderFooter, Document document, boolean header, OpenPdfHelper docHelper ) throws DocumentException { + List list = new ArrayList<>(); + Iterator itDoc = docHeaderFooter.docElements(); + while ( itDoc.hasNext() ) { + list.add( itDoc.next() ); + } + Element[] e = new Element[ list.size() ]; + SafeFunction.apply( () -> { + for ( int k=0; k this.testDocWorker( "default_doc_fail1" , DocConfig.TYPE_PDF ) ); + } + + @Test + public void testCustomFont() { + BaseFont font = OpenPpfDocHandler.findFont( CUSTOM_FONT ); + Assert.assertNotNull(font); + } + + @Test + public void testOpenPDF() { + boolean ok = this.testDocWorker( DEFAULT_DOC , DocConfig.TYPE_PDF ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenHTML() { + boolean ok = this.testDocWorker( DEFAULT_DOC , DocConfig.TYPE_HTML ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenRTF() { + boolean ok = this.testDocWorker( DEFAULT_DOC , DocConfig.TYPE_RTF ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenAltPDF() { + boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , DocConfig.TYPE_PDF ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenAltHTML() { + boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , DocConfig.TYPE_HTML ); + Assert.assertTrue(ok); + } + + @Test + public void testOpenAltRTF() { + boolean ok = this.testDocWorker( DEFAULT_DOC_ALT , DocConfig.TYPE_RTF ); + Assert.assertTrue(ok); + } + +} diff --git a/fj-doc-mod-openrtf-ext/src/test/java/test/org/fugerit/java/doc/mod/openrtf/ext/TestDocBase.java b/fj-doc-mod-openrtf-ext/src/test/java/test/org/fugerit/java/doc/mod/openrtf/ext/TestDocBase.java new file mode 100644 index 000000000..0cd4ca1a1 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/test/java/test/org/fugerit/java/doc/mod/openrtf/ext/TestDocBase.java @@ -0,0 +1,42 @@ +package test.org.fugerit.java.doc.mod.openrtf.ext; + +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; + +import org.fugerit.java.core.lang.helpers.ClassHelper; +import org.fugerit.java.doc.base.config.DocInput; +import org.fugerit.java.doc.base.config.DocOutput; +import org.fugerit.java.doc.base.config.DocTypeHandler; + +import lombok.extern.slf4j.Slf4j; +import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfig; +import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfigFacade; + +@Slf4j +public class TestDocBase { + + private FreemarkerDocProcessConfig config = FreemarkerDocProcessConfigFacade.loadConfigSafe( "cl://fm-test-mod-openpdf-config.xml" ); + + protected boolean testDocWorker( String testCase, String type ) { + boolean ok = false; + String inputXml = "xml/"+testCase+".xml" ; + DocTypeHandler handler = this.config.getFacade().findHandler( type ); + File outputFile = new File( "target", testCase+"."+handler.getType() ); + log.info( "inputXml:{}, outputFile:{}", inputXml, outputFile ); + try ( InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( inputXml ) ); + OutputStream os = new FileOutputStream( outputFile ) ) { + handler.handle( DocInput.newInput( handler.getType() , reader ) , DocOutput.newOutput(os) ); + ok = true; + } catch (Exception e) { + String message = "Error : "+e.getMessage(); + log.error( message , e ); + fail( message ); + } + return ok; + } + +} diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/fm-test-mod-openpdf-config.xml b/fj-doc-mod-openrtf-ext/src/test/resources/fm-test-mod-openpdf-config.xml new file mode 100644 index 000000000..aa6bb74f9 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/test/resources/fm-test-mod-openpdf-config.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Bold.ttf b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Bold.ttf new file mode 100644 index 000000000..b51a4d638 Binary files /dev/null and b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Bold.ttf differ diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-BoldItalic.ttf b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-BoldItalic.ttf new file mode 100644 index 000000000..d0fe23bed Binary files /dev/null and b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-BoldItalic.ttf differ diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Italic.ttf b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Italic.ttf new file mode 100644 index 000000000..266d5cb81 Binary files /dev/null and b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Italic.ttf differ diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Regular.ttf b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Regular.ttf new file mode 100644 index 000000000..a54ad4b93 Binary files /dev/null and b/fj-doc-mod-openrtf-ext/src/test/resources/font/TitilliumWeb-Regular.ttf differ diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/log4j2.xml b/fj-doc-mod-openrtf-ext/src/test/resources/log4j2.xml new file mode 100644 index 000000000..51b9129fc --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/test/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/test/img_test_teal.jpg b/fj-doc-mod-openrtf-ext/src/test/resources/test/img_test_teal.jpg new file mode 100644 index 000000000..a8213e159 Binary files /dev/null and b/fj-doc-mod-openrtf-ext/src/test/resources/test/img_test_teal.jpg differ diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc.xml b/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc.xml new file mode 100644 index 000000000..c60ac3737 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc.xml @@ -0,0 +1,58 @@ + + + + + + 10;10;10;30 + excel-table=print + + Basic example + fj doc venus sample source xml + fugerit79 + en + + true + /css/test.css + + header test + + + test + ${r"${currentPage}"} / ${r"${pageCount}"} + test + + + + Test times roman + Courier + Symbol + Symbol + Test default font + + + Name + Surname + Title + + + + + + + + + + + + + + + + +
+ + +
\ No newline at end of file diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc_alt.xml b/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc_alt.xml new file mode 100644 index 000000000..1908770f3 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc_alt.xml @@ -0,0 +1,61 @@ + + + + + + 10;10;10;30 + excel-table=print + + Basic example + fj doc venus sample source xml + fugerit79 + en + + horizontal +
+ header test +
+
+ ${r"${currentPage}"} / ${r"${pageCount}"} +
+
+ + + My sample title + + + Name + Surname + Title + + + + + + + + + + + + + + + +
+ + 1 + 2 + +
+ + + + + + + +
\ No newline at end of file diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc_fail1.xml b/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc_fail1.xml new file mode 100644 index 000000000..f8335b706 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/test/resources/xml/default_doc_fail1.xml @@ -0,0 +1,58 @@ + + + + + + 10;10;10;30 + excel-table=print + + Basic example + fj doc venus sample source xml + fugerit79 + en + + true + /css/test.css + + header test + + + test + ${r"${currentPage}"} / ${r"${pageCount}"} + + + + + Test times roman + Courier + Symbol + Symbol + Test default font + + + Name + Surname + Title + + + + + + + + + + + + + + + + +
+ + +
\ No newline at end of file diff --git a/fj-doc-mod-openrtf-ext/src/test/resources/xml/test.xml b/fj-doc-mod-openrtf-ext/src/test/resources/xml/test.xml new file mode 100644 index 000000000..f8d34b0a1 --- /dev/null +++ b/fj-doc-mod-openrtf-ext/src/test/resources/xml/test.xml @@ -0,0 +1,44 @@ + + + + + + TitilliumWeb + 10 + 50;50;20;20 + 1 + Test doc + Fugerit + Venus + en + + + + + + + + + + + +
+
+ + + ${currentPage} / ${pageCount} + + + + Bookmark 1 + + +
+ + MAIN TITLE + Secondary title + +
\ No newline at end of file diff --git a/fj-doc-playground-quarkus/pom.xml b/fj-doc-playground-quarkus/pom.xml index 0f904a2cc..2b84af201 100644 --- a/fj-doc-playground-quarkus/pom.xml +++ b/fj-doc-playground-quarkus/pom.xml @@ -82,11 +82,6 @@ - - org.fugerit.java - fj-doc-mod-openpdf - ${fj-doc-mod-openpdf-version} - diff --git a/fj-doc-playground-quarkus/src/main/resources/playground-config/fm-playground-doc-process.xml b/fj-doc-playground-quarkus/src/main/resources/playground-config/fm-playground-doc-process.xml index 1dd4c4b56..dfff8f56a 100644 --- a/fj-doc-playground-quarkus/src/main/resources/playground-config/fm-playground-doc-process.xml +++ b/fj-doc-playground-quarkus/src/main/resources/playground-config/fm-playground-doc-process.xml @@ -5,12 +5,12 @@ xsi:schemaLocation="https://freemarkerdocprocess.fugerit.org https://www.fugerit.org/data/java/doc/xsd/freemarker-doc-process-1-0.xsd" > - + - + diff --git a/fj-doc-sample/pom.xml b/fj-doc-sample/pom.xml index 43ef209e3..9a20dc1f6 100644 --- a/fj-doc-sample/pom.xml +++ b/fj-doc-sample/pom.xml @@ -74,9 +74,12 @@ org.fugerit.java - fj-doc-mod-openpdf - ${fj-doc-mod-openpdf-version} - test + fj-doc-mod-openpdf-ext + + + + org.fugerit.java + fj-doc-mod-openrtf-ext diff --git a/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/dev/TestItext2.java b/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/dev/TestItext2.java index cb6b78325..57c2ca724 100644 --- a/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/dev/TestItext2.java +++ b/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/dev/TestItext2.java @@ -13,7 +13,7 @@ import org.fugerit.java.doc.base.config.DocTypeHandler; import org.fugerit.java.doc.base.facade.DocFacade; import org.fugerit.java.doc.base.model.DocBase; -import org.fugerit.java.doc.mod.openpdf.PdfTypeHandler; +import org.fugerit.java.doc.mod.openpdf.ext.PdfTypeHandler; import org.junit.Assert; import org.junit.Test; diff --git a/pom.xml b/pom.xml index b883e64d3..a23a3bbe9 100644 --- a/pom.xml +++ b/pom.xml @@ -31,8 +31,6 @@ 4.0.1 ${poi-version} - - 1.3.0 @@ -98,6 +96,8 @@ fj-doc-mod-poi fj-doc-mod-fop fj-doc-mod-opencsv + fj-doc-mod-openpdf-ext + fj-doc-mod-openrtf-ext fj-doc-val-core fj-doc-val-pdfbox fj-doc-val-poi @@ -166,6 +166,18 @@ ${fj-doc-version} + + org.fugerit.java + fj-doc-mod-openpdf-ext + ${fj-doc-version} + + + + org.fugerit.java + fj-doc-mod-openrtf-ext + ${fj-doc-version} + + org.fugerit.java fj-doc-lib-simpletable