From 02d0dfe0e8fdbb47c1787469a9bb7e89e456e2d6 Mon Sep 17 00:00:00 2001 From: IntlUser Date: Mon, 1 Jun 2020 16:27:59 +0300 Subject: [PATCH 1/5] Issue #37 fix --- .github/workflows/release.yml | 34 ++--- Makefile | 2 +- README.adoc | 10 +- pom.xml | 2 +- src/main/java/com/metanorma/fop/mn2pdf.java | 135 ++++++++++++++++---- 5 files changed, 131 insertions(+), 52 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cb4e2fa9..8fe62ba5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -46,16 +46,16 @@ jobs: run: | make test - - name: Deploy to Maven - env: - # for download barcode4j - GH_TOKEN: ${{ github.token }} - GH_USERNAME: ${{ github.actor }} - # for deploy mn2pdf - GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} - GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }} - run: | - make deploy +# - name: Deploy to Maven +# env: +# # for download barcode4j +# GH_TOKEN: ${{ github.token }} +# GH_USERNAME: ${{ github.actor }} +# # for deploy mn2pdf +# GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} +# GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }} +# run: | +# make deploy - name: Create Release id: create_release @@ -86,10 +86,10 @@ jobs: asset_name: mn2pdf-${{ steps.get_version.outputs.VERSION }}.jar asset_content_type: application/java-archive - - name: Notify mn2pdf-ruby - uses: peter-evans/repository-dispatch@v1 - with: - token: ${{ secrets.PAT_TOKEN }} - repository: metanorma/mn2pdf-ruby - event-type: metanorma/mn2pdf - client-payload: '{ "ref": "${{ github.ref }}" }' +# - name: Notify mn2pdf-ruby +# uses: peter-evans/repository-dispatch@v1 +# with: +# token: ${{ secrets.PAT_TOKEN }} +# repository: metanorma/mn2pdf-ruby +# event-type: metanorma/mn2pdf +# client-payload: '{ "ref": "${{ github.ref }}" }' diff --git a/Makefile b/Makefile index 9a981144..d988c219 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ SHELL ?= /bin/bash #JAR_VERSION := $(shell mvn -q -Dexec.executable="echo" -Dexec.args='$${project.version}' --non-recursive exec:exec -DforceStdout) -JAR_VERSION := 1.16 +JAR_VERSION := 1.17 JAR_FILE := mn2pdf-$(JAR_VERSION).jar all: target/$(JAR_FILE) diff --git a/README.adoc b/README.adoc index f0108353..2a0aa7e6 100644 --- a/README.adoc +++ b/README.adoc @@ -15,14 +15,14 @@ You will need the `maven` build tool and `make`. [source,sh] ---- -java -Xss5m -Xmx1024m -jar target/mn2pdf-1.16.jar --xml-file --xsl-file --pdf-file +java -Xss5m -Xmx1024m -jar target/mn2pdf-1.17.jar --xml-file --xsl-file --pdf-file ---- e.g. [source,sh] ---- -java -Xss5m -Xmx1024m -jar target/mn2pdf-1.16.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf +java -Xss5m -Xmx1024m -jar target/mn2pdf-1.17.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf ---- @@ -42,7 +42,7 @@ Update version in `pom.xml`, e.g.: ---- com.metanorma.fop mn2pdf -1.16 +1.17 Metanorma XML to PDF converter ---- @@ -53,8 +53,8 @@ Tag the same version in Git: [source,xml] ---- -git tag v1.12 -git push origin v1.12 +git tag v1.17 +git push origin v1.17 ---- Then the corresponding GitHub release will be automatically created at: diff --git a/pom.xml b/pom.xml index 84387228..1fc088d0 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.metanorma.fop mn2pdf - 1.17 + 1.17b Metanorma XML to PDF converter jar https://www.metanorma.com diff --git a/src/main/java/com/metanorma/fop/mn2pdf.java b/src/main/java/com/metanorma/fop/mn2pdf.java index 70c5c1c7..b79cef77 100644 --- a/src/main/java/com/metanorma/fop/mn2pdf.java +++ b/src/main/java/com/metanorma/fop/mn2pdf.java @@ -10,6 +10,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; +import java.io.StringReader; import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; @@ -25,6 +26,8 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.ErrorListener; +//import javax.xml.transform.ErrorListener; +import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; @@ -43,6 +46,9 @@ import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; +import org.apache.fop.events.Event; +import org.apache.fop.events.model.EventSeverity; +import org.apache.fop.events.EventFormatter; import net.sourceforge.jeuclid.fop.plugin.JEuclidFopFactoryConfigurator; @@ -53,6 +59,7 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; + import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -151,17 +158,17 @@ public class mn2pdf { * @throws FOPException, SAXException In case of a FOP problem */ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws IOException, FOPException, SAXException, TransformerException, TransformerConfigurationException, TransformerConfigurationException, ParserConfigurationException { - - String imagesxml = getImageFilePath(xml); try { //Setup XSLT - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer transformer = factory.newTransformer(new StreamSource(xsl)); - - transformer.setParameter("svg_images", imagesxml); + TransformerFactory factoryFO = TransformerFactory.newInstance(); + Transformer transformerFO = factoryFO.newTransformer(new StreamSource(xsl)); + + transformerFO.setOutputProperty(OutputKeys.ENCODING, "UTF-16"); // to fix issue with UTF-16 surrogate pairs + + transformerFO.setParameter("svg_images", imagesxml); //Setup input for XSLT transformation Source src = new StreamSource(xml); @@ -172,38 +179,40 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws StringWriter resultWriter = new StringWriter(); StreamResult sr = new StreamResult(resultWriter); //Start XSLT transformation and FO generating - transformer.transform(src, sr); + transformerFO.transform(src, sr); String xmlFO = resultWriter.toString(); if (DEBUG) { - //DEBUG: write intermediate FO to file - //BufferedWriter writer = new BufferedWriter(new FileWriter(pdf.getAbsolutePath() + ".fo.xml")); - BufferedWriter writer = Files.newBufferedWriter(Paths.get(pdf.getAbsolutePath() + ".fo.xml")); - - writer.write(xmlFO); - writer.close(); + //DEBUG: write intermediate FO to file + try ( + BufferedWriter writer = Files.newBufferedWriter(Paths.get(pdf.getAbsolutePath() + ".fo.xml"))) { + writer.write(xmlFO); + } //Setup output //OutputStream outstream = new java.io.FileOutputStream(pdf.getAbsolutePath() + ".fo.xml"); //Resulting SAX events (the generated FO) must be piped through to FOP //Result res = new StreamResult(outstream); //Start XSLT transformation and FO generating //transformer.transform(src, res); - - // using resultWriter - //StringWriter resultWriter = new StringWriter(); - //StreamResult sr = new StreamResult(resultWriter); - //transformer.transform(src, sr); } fontConfig fontcfg = new fontConfig(xmlFO, fontPath); + // FO processing by FOP + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); // identity transformer + src = new StreamSource(new StringReader(xmlFO)); + runFOP(fontcfg, src, pdf, transformer); + if(PDFUA_error) { System.out.println("WARNING: Trying to generate PDF in non PDF/UA-1 mode."); fontcfg.setPDFUAmode("DISABLED"); + src = new StreamSource(new StringReader(xmlFO)); runFOP(fontcfg, src, pdf, transformer); System.out.println("WARNING: PDF generated in non PDF/UA-1 mode."); } + for(String msg: fontcfg.getMessages()) { System.out.println(msg); } @@ -224,7 +233,6 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws } } } - private void runFOP (fontConfig fontcfg, Source src, File pdf, Transformer transformer) throws IOException, FOPException, SAXException, TransformerException, TransformerConfigurationException, TransformerConfigurationException { OutputStream out = null; try { @@ -235,6 +243,11 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf, Transformer trans JEuclidFopFactoryConfigurator.configure(fopFactory); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); // configure foUserAgent + + //Adding a simple logging listener that writes to stdout and stderr + //foUserAgent.getEventBroadcaster().addEventListener(new SysOutEventListener()); + // Add your own event listener + //foUserAgent.getEventBroadcaster().addEventListener(new MyEventListener()); // Setup output stream. Note: Using BufferedOutputStream // for performance reasons (helpful with FileOutputStreams). @@ -248,19 +261,26 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf, Transformer trans //factory = TransformerFactory.newInstance(); //transformer = factory.newTransformer(); // identity transformer - // Setup input stream - //Source srcFO = new StreamSource(new StringReader(xmlFO)); - + // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); - + transformer.setErrorListener(new DefaultErrorListener()); - + // Start XSLT transformation and FOP processing - transformer.transform(src, res); + // Setup input stream + transformer.transform(src, res); + } catch (Exception e) { - e.printStackTrace(System.err); - System.exit(ERROR_EXIT_CODE); + String excstr=e.toString(); + if (excstr.contains("PDFConformanceException") && excstr.contains("PDF/UA-1") && !PDFUA_error) { // excstr.contains("all fonts, even the base 14 fonts, have to be embedded") + System.err.println(e.toString()); + PDFUA_error = true; + } else { + e.printStackTrace(System.err); + System.exit(ERROR_EXIT_CODE); + } + } finally { if (out != null) { out.close(); @@ -268,7 +288,7 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf, Transformer trans } } - private class DefaultErrorListener implements ErrorListener { + private class DefaultErrorListener implements javax.xml.transform.ErrorListener { public void warning(TransformerException exc) { System.err.println(exc.toString()); @@ -291,6 +311,65 @@ public void fatalError(TransformerException exc) } } + private static class MyEventListener implements org.apache.fop.events.EventListener { + + public void processEvent(Event event) { + if ("org.apache.fop.layoutmgr.BlockLevelEventProducer.overconstrainedAdjustEndIndent". + equals(event.getEventID())) { + //skip + } else + if("org.apache.fop.render.RendererEventProducer.endPage". + equals(event.getEventID())) { + //skip + }else + if ("org.apache.fop.pdf.PDFConformanceException". + equals(event.getEventID())) { + System.err.println(new RuntimeException(EventFormatter.format(event)).toString()); + PDFUA_error = true; + } + else + if ("org.apache.fop.ResourceEventProducer.imageNotFound" + .equals(event.getEventID())) { + + //Get the FileNotFoundException that's part of the event's parameters + //FileNotFoundException fnfe = (FileNotFoundException)event.getParam("fnfe"); + + System.out.println("---=== imageNotFound Event for " + event.getParam("uri") + + "!!! ===---"); + //Stop processing when an image could not be found. Otherwise, FOP would just + //continue without the image! + + System.out.println("Throwing a RuntimeException..."); + //throw new RuntimeException(EventFormatter.format(event), fnfe); + } else { + //ignore all other events + } + } + + } + + /** A simple event listener that writes the events to stdout and sterr. */ + private static class SysOutEventListener implements org.apache.fop.events.EventListener { + + /** {@inheritDoc} */ + public void processEvent(Event event) { + String msg = EventFormatter.format(event); + EventSeverity severity = event.getSeverity(); + if (severity == EventSeverity.INFO) { + System.out.println("[INFO ] " + msg); + } else if (severity == EventSeverity.WARN) { + System.out.println("[WARN ] " + msg); + } else if (severity == EventSeverity.ERROR) { + System.err.println("[ERROR] " + msg); + } else if (severity == EventSeverity.FATAL) { + System.err.println("[FATAL] " + msg); + } else { + assert false; + } + } + } + + /** * Main method. From feffc895a7dadb01117f56f63dad2c9b83cf7380 Mon Sep 17 00:00:00 2001 From: IntlUser Date: Mon, 1 Jun 2020 17:37:13 +0300 Subject: [PATCH 2/5] Vista fonts install commented --- .github/workflows/macos.yml | 12 ++++++------ .github/workflows/release.yml | 6 +++--- .github/workflows/ubuntu.yml | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 93ce4048..36ef37c3 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -26,12 +26,12 @@ jobs: # Install cabextract brew install cabextract - - name: Install Microsoft Vista fonts - run: | - export MN_PDF_FONT_PATH=${GITHUB_WORKSPACE}/fonts - export MS_FONT_PATH=${MN_PDF_FONT_PATH}/truetype/vista - export RENAME_FONTS=false - curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | bash +# - name: Install Microsoft Vista fonts +# run: | +# export MN_PDF_FONT_PATH=${GITHUB_WORKSPACE}/fonts +# export MS_FONT_PATH=${MN_PDF_FONT_PATH}/truetype/vista +# export RENAME_FONTS=false +# curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | bash - name: Build env: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8fe62ba5..8289c228 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -26,9 +26,9 @@ jobs: echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections sudo apt-get install ttf-mscorefonts-installer - - name: Setup Cambria fonts - run: | - curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash +# - name: Setup Cambria fonts +# run: | +# curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash - uses: actions/checkout@master diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 78ad57a6..8d9b4b80 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -27,9 +27,9 @@ jobs: echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections sudo apt-get install ttf-mscorefonts-installer - - name: Setup Cambria fonts - run: | - curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash +# - name: Setup Cambria fonts +# run: | +# curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash - name: Build env: From 54a432093283c90370e90e2c17686f6bca3356a1 Mon Sep 17 00:00:00 2001 From: IntlUser Date: Mon, 1 Jun 2020 17:42:13 +0300 Subject: [PATCH 3/5] macos.yml updated --- .github/workflows/macos.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 36ef37c3..566ee563 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -45,6 +45,4 @@ jobs: GH_TOKEN: ${{ github.token }} GH_USERNAME: ${{ github.actor }} run: | - export MN_PDF_FONT_PATH=${GITHUB_WORKSPACE}/fonts - ls -alR ${MN_PDF_FONT_PATH} make test From bb82d5a9a2d8b114ff089af1143ffd8ce7fcbd8c Mon Sep 17 00:00:00 2001 From: IntlUser Date: Mon, 1 Jun 2020 19:22:43 +0300 Subject: [PATCH 4/5] Fonts added for #38 --- src/main/resources/pdf_fonts_config.xml | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/resources/pdf_fonts_config.xml b/src/main/resources/pdf_fonts_config.xml index eb4db767..da226eda 100644 --- a/src/main/resources/pdf_fonts_config.xml +++ b/src/main/resources/pdf_fonts_config.xml @@ -306,6 +306,53 @@ Relative config URLs are resolved relative to location of this file. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From a75531a3e12dc2f90305eb57b056f2f63bcc051f Mon Sep 17 00:00:00 2001 From: IntlUser Date: Mon, 1 Jun 2020 23:31:10 +0300 Subject: [PATCH 5/5] version updated to 1.17 --- .github/workflows/release.yml | 34 +++++++++++++++++----------------- pom.xml | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8289c228..12ffdd86 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -46,16 +46,16 @@ jobs: run: | make test -# - name: Deploy to Maven -# env: -# # for download barcode4j -# GH_TOKEN: ${{ github.token }} -# GH_USERNAME: ${{ github.actor }} -# # for deploy mn2pdf -# GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} -# GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }} -# run: | -# make deploy + - name: Deploy to Maven + env: + # for download barcode4j + GH_TOKEN: ${{ github.token }} + GH_USERNAME: ${{ github.actor }} + # for deploy mn2pdf + GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} + GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }} + run: | + make deploy - name: Create Release id: create_release @@ -86,10 +86,10 @@ jobs: asset_name: mn2pdf-${{ steps.get_version.outputs.VERSION }}.jar asset_content_type: application/java-archive -# - name: Notify mn2pdf-ruby -# uses: peter-evans/repository-dispatch@v1 -# with: -# token: ${{ secrets.PAT_TOKEN }} -# repository: metanorma/mn2pdf-ruby -# event-type: metanorma/mn2pdf -# client-payload: '{ "ref": "${{ github.ref }}" }' + - name: Notify mn2pdf-ruby + uses: peter-evans/repository-dispatch@v1 + with: + token: ${{ secrets.PAT_TOKEN }} + repository: metanorma/mn2pdf-ruby + event-type: metanorma/mn2pdf + client-payload: '{ "ref": "${{ github.ref }}" }' diff --git a/pom.xml b/pom.xml index 1fc088d0..84387228 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.metanorma.fop mn2pdf - 1.17b + 1.17 Metanorma XML to PDF converter jar https://www.metanorma.com