Skip to content

Commit

Permalink
Merge pull request #113 from fugerit-org/111-add-module-openpdf-and-o…
Browse files Browse the repository at this point in the history
…penrtf

Added module openpdf-ext and openrtf-ext #111
  • Loading branch information
fugerit79 authored Feb 4, 2024
2 parents 59017c4 + 37fd1c9 commit 8279da4
Show file tree
Hide file tree
Showing 58 changed files with 2,364 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion fj-doc-mod-opencsv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion fj-doc-mod-opencsv/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<name>fj-doc-mod-opencsv</name>
<description>Rendere for CSV using OpenCSV</description>
<description>Renderer for CSV using OpenCSV</description>

<licenses>
<license>
Expand Down
22 changes: 22 additions & 0 deletions fj-doc-mod-openpdf-ext/README.md
Original file line number Diff line number Diff line change
@@ -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.
61 changes: 61 additions & 0 deletions fj-doc-mod-openpdf-ext/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>fj-doc-mod-openpdf-ext</artifactId>

<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>3.3.2-SNAPSHOT</version>
</parent>

<name>fj-doc-mod-openpdf-ext</name>
<description>Renderer for PDF format, based on https://github.com/LibrePDF/OpenPDF</description>

<properties>
<openpdf-version-version>1.3.39</openpdf-version-version>
</properties>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<dependencies>

<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>${openpdf-version-version}</version>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-core</artifactId>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc-base</artifactId>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc-freemarker</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<organization>
<url>https://www.fugerit.org</url>
<name>Fugerit</name>
</organization>

<url>https://www.fugerit.org/perm/venus/</url>

</project>
Original file line number Diff line number Diff line change
@@ -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() );
}

}
Original file line number Diff line number Diff line change
@@ -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() );
}
}
Original file line number Diff line number Diff line change
@@ -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 );
}

}
Original file line number Diff line number Diff line change
@@ -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 );
}

}
Original file line number Diff line number Diff line change
@@ -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<fontList.getLength(); k++ ) {
Element currentFontTag = (Element) fontList.item( k );
String name = currentFontTag.getAttribute( ATT_NAME );
String path = currentFontTag.getAttribute( ATT_PATH );
log.info( "current font {} - {}", name, path );
ConfigException.apply( () -> OpenPpfDocHandler.registerFont( name , path ) );
}
}

}
Loading

0 comments on commit 8279da4

Please sign in to comment.