Skip to content

Commit

Permalink
added ddi-pdf exporter written in Jython
Browse files Browse the repository at this point in the history
  • Loading branch information
ErykKul committed Jul 6, 2024
1 parent 9ae6875 commit 11721d8
Show file tree
Hide file tree
Showing 28 changed files with 11,458 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target
.vscode
.idea
.flattened-pom.xml
pom.xml.versionsBackup
pom.xml.versionsBackup
src/test/resources/fop/output.pdf
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Supported Dataverse versions: 6.0 - recent.

## Installation

If you haven’t already configured it, set the [dataverse-spi-exporters-directory](https://guides.dataverse.org/en/latest/installation/config.html#dataverse-spi-exporters-directory) configuration value first. Then navigate to the configured directory and download the [JAR file](https://repo1.maven.org/maven2/io/github/erykkul/dataverse-transformer-exporter/1.0.4/dataverse-transformer-exporter-1.0.4-jar-with-dependencies.jar) together with the examples you want to try out:
If you haven’t already configured it, set the [dataverse-spi-exporters-directory](https://guides.dataverse.org/en/latest/installation/config.html#dataverse-spi-exporters-directory) configuration value first. Then navigate to the configured directory and download the [JAR file](https://repo1.maven.org/maven2/io/github/erykkul/dataverse-transformer-exporter/1.0.5/dataverse-transformer-exporter-1.0.5-jar-with-dependencies.jar) together with the examples you want to try out:

```shell
# download the jar
wget -O transformer-exporter-1.0.4.jar https://repo1.maven.org/maven2/io/github/erykkul/dataverse-transformer-exporter/1.0.4/dataverse-transformer-exporter-1.0.4-jar-with-dependencies.jar
wget -O transformer-exporter-1.0.5.jar https://repo1.maven.org/maven2/io/github/erykkul/dataverse-transformer-exporter/1.0.5/dataverse-transformer-exporter-1.0.5-jar-with-dependencies.jar
# download the hello-world example
mkdir hello-world
wget -O hello-world/config.json https://raw.githubusercontent.com/erykkul/dataverse-transformer-exporter/main/examples/hello-world/config.json
Expand Down Expand Up @@ -102,6 +102,10 @@ This exporter copies only the title, the author names and the filenames of the d

This exporter takes JSON input from a prerequisite exporter (`short_example_py` by default), and displays it as HTML. It is written in Python. In order to change the JSON input for this exporter, change the `prerequisiteFormatName` value in the `config.json` to the format name of the exporter you wish to use as input.

### DDI PDF codebook

This exporter is entirely based on the [DDI PDF Exporter](https://github.com/gdcc/exporter-ddipdf). It is simply a port of that exporter into Python (Jython). It illustrates how to convert XML input to PDF in an exporter.

## Developer guide

The easiest way to start is to write JavasCript code. You can use the provided [Croissant](/examples/croissant/js/croissant.js) code as the start point. You will need to restart the server after changing that code. Note that the exporters use caching, you will need to either to wait until the cache is expired or delete the cached exporter output manually to see the changes.
Expand Down
8 changes: 8 additions & 0 deletions examples/ddi-pdf/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"formatName": "ddi_pdf",
"harvestable": false,
"availableToUsers": true,
"mediaType": "application/pdf",
"displayName": "DDI pdf codebook",
"prerequisiteFormatName": "ddi"
}
31 changes: 31 additions & 0 deletions examples/ddi-pdf/transformer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from java.io import ByteArrayInputStream, ByteArrayOutputStream, File
from java.lang import System
from java.util import Base64
from javax.xml.transform import TransformerFactory
from javax.xml.transform.sax import SAXResult
from javax.xml.transform.stream import StreamSource
from org.apache.fop.apps import FopFactory, MimeConstants

localeEnvVar = System.getenv().get("LANG") if System.getenv().get("LANG") else "en"
if localeEnvVar.index(".") > 0:
localeEnvVar = localeEnvVar[0 : localeEnvVar.index(".")]
if localeEnvVar.index("_") > 0:
localeEnvVar = localeEnvVar[0 : localeEnvVar.index("_")]

fopFactory = FopFactory.newInstance(File(".").toURI())
foUserAgent = fopFactory.newFOUserAgent()
out = ByteArrayOutputStream()
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out)

factory = TransformerFactory.newInstance()
factory.setURIResolver(
lambda href, base: StreamSource(File(path + "/xslt/" + href))
)
transformer = factory.newTransformer(StreamSource(File(path + "/xslt/ddi-to-fo.xsl")))
transformer.setParameter("language-code", localeEnvVar)
src = StreamSource(ByteArrayInputStream(Base64.getDecoder().decode(x["base64"])))
saxRes = SAXResult(fop.getDefaultHandler())

transformer.transform(src, saxRes)

res["base64"] = Base64.getEncoder().encodeToString(out.toByteArray())
Loading

0 comments on commit 11721d8

Please sign in to comment.