Permalink
Browse files

Apache FOP

  • Loading branch information...
fpaparoni committed Feb 5, 2018
1 parent 85485fc commit 5e12cb77768f497141fac8b46c365a7c723370fe
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<rubrica>
<proprietario>Federico</proprietario>
<contatto>
<nome>Andrea</nome>
<cognome>Bianchi</cognome>
<numero>1234567890</numero>
<email>andrea.bianchi@email</email>
</contatto>
<contatto>
<nome>Pino</nome>
<cognome>Verdi</cognome>
<numero>20304050</numero>
<email>pino.verdi@email</email>
</contatto>
<contatto>
<nome>Mario</nome>
<cognome>Rossi</cognome>
<numero>987654321</numero>
<email>mario.rossi@email</email>
</contatto>
</rubrica>
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
<!-- ========================= -->
<!-- root element: rubrica -->
<!-- ========================= -->
<xsl:template match="rubrica">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="16pt" font-weight="bold" space-after="5mm">Rubrica di <xsl:value-of select="proprietario"/>
</fo:block>
<fo:block font-size="10pt">
<fo:table table-layout="fixed" width="100%" border-collapse="separate">
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-body>
<xsl:apply-templates select="contatto"/>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- ========================= -->
<!-- child element: contatto -->
<!-- ========================= -->
<xsl:template match="contatto">
<fo:table-row>
<fo:table-cell>
<fo:block>
<xsl:value-of select="nome"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="cognome"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="numero"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="email"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>
130 pom.xml
@@ -1,70 +1,70 @@
<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>
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>
<groupId>com.javastaff</groupId>
<artifactId>html2pdf</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<groupId>com.javastaff</groupId>
<artifactId>html2pdf</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>html2pdf</name>
<name>html2pdf</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- FlyingSaucer -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.11</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<!-- iText -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.12</version>
</dependency>
<!-- PD4ML -->
<dependency>
<groupId>org.zefer.pd4ml</groupId>
<artifactId>pd4ml</artifactId>
<version>370fx2</version>
</dependency>
<dependency>
<groupId>org.zefer.pd4ml</groupId>
<artifactId>ss_css2</artifactId>
<version>370fx2</version>
</dependency>
<!-- JODConverter -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-online</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>pd4ml-repo</id>
<name>your custom repo</name>
<url>http://maven.scandilabs.com/repository</url>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- FlyingSaucer -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.11</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<!-- iText -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.12</version>
</dependency>
<!-- PD4ML -->
<dependency>
<groupId>org.zefer.pd4ml</groupId>
<artifactId>pd4ml</artifactId>
<version>370fx2</version>
</dependency>
<dependency>
<groupId>org.zefer.pd4ml</groupId>
<artifactId>ss_css2</artifactId>
<version>370fx2</version>
</dependency>
<!-- Apache FOP -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>pd4ml-repo</id>
<name>your custom repo</name>
<url>http://maven.scandilabs.com/repository</url>
</repository>
</repositories>
</project>
@@ -0,0 +1,53 @@
package com.javastaff.html2pdf;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
public class ApacheFOPTest {
public static void main(String a[]) throws URISyntaxException, FileNotFoundException {
try {
// Setup input and output files
String xmlfile = "https://raw.githubusercontent.com/fpaparoni/html2pdf/master/html/test.xml";
String xsltfile = "https://raw.githubusercontent.com/fpaparoni/html2pdf/master/html/test2fo.xsl";
File pdffile = new File("test-apachefop.pdf");
// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(new URL(xsltfile).openStream()));
Source src = new StreamSource(new URL(xmlfile).openStream());
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} finally {
out.close();
}
System.out.println("ApacheFOP test created");
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
}
@@ -84,7 +84,6 @@ public CustomImageProvider(String baseUrl) {
@Override
public Image retrieve(String src) {
System.out.println("PASSO DI QUI "+src);
int pos = src.indexOf("base64,");
try {
if (src.startsWith("data") && pos > 0) {
@@ -103,9 +102,7 @@ public Image retrieve(String src) {
}
}
@Override
public String getImageRootPath() {
System.out.println("PASSO DI QUA ");
return baseUrl;
}
}

This file was deleted.

Oops, something went wrong.

0 comments on commit 5e12cb7

Please sign in to comment.