attempt to be able to convert to pdf

This commit is contained in:
jstaerk
2023-08-14 11:07:13 +02:00
parent 3e81c7036f
commit 9bfc1ef7fe
11 changed files with 199 additions and 69 deletions

View File

@@ -19,10 +19,7 @@
*********************************************************************** */
package org.mustangproject.ZUGFeRD;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.*;
import org.apache.xmlgraphics.util.MimeConstants;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
@@ -35,7 +32,6 @@ import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class VisualizationTest extends ResourceCase {
@@ -162,76 +158,16 @@ public class VisualizationTest extends ResourceCase {
/* remove file endings so that tests can also pass after checking
out from git with arbitrary options (which may include CSRF changes)
*/
result = zvi.toFOP(CIIinputFile.getAbsolutePath());
File expectedResult=getResourceAsFile("fop.xml");
ByteArrayOutputStream fopout = new ByteArrayOutputStream();
//FileOutputStream outfile = new FileOutputStream("c:\\Users\\jstaerk\\temp\\fop2.pdf");
// File out=new File("c:\\Users\\jstaerk\\temp\\fop.xml");
// Step 1: Construct a FopFactory by specifying a reference to the configuration file
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = FopFactory.newInstance(new File("c:\\Users\\jstaerk\\temp\\fop.xconf"));
FOUserAgent userAgent = fopFactory.newFOUserAgent();
userAgent.getRendererOptions().put("pdf-a-mode", "PDF/A-1b");
// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("c:\\Users\\jstaerk\\temp\\fop3.pdf")));
try {
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);
// Step 4: Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source src = new StreamSource(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Step 6: Start XSLT transformation and FOP processing
transformer.transform(src, res);
//Files.write(Paths.get("C:\\Users\\jstaerk\\temp\\fop.pdf"), res.toString().getBytes(StandardCharsets.UTF_8));
} finally {
//Clean-up
out.close();
}
// Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, fopout);
/*
Transformer transformer = TransformerFactory.newInstance().newTransformer(
new StreamSource(new File(args[1])));
transformer.transform(new StreamSource(new File(args[0])),
new SAXResult(((Fop) fop).getDefaultHandler()));
*/
// fopout.toByteArray();
expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r","").replace("\n","");
// remove linebreaks as well...
zvi.toPDF(CIIinputFile.getAbsolutePath(), "c:\\users\\jstaerk\\temp\\fopy2.pdf");
} catch (UnsupportedOperationException e) {
fail("UnsupportedOperationException should not happen: "+e.getMessage());
} catch (IllegalArgumentException e) {
fail("IllegalArgumentException should not happen: "+e.getMessage());
} catch (TransformerException e) {
fail("TransformerException should not happen: "+e.getMessage());
} catch (IOException e) {
fail("IOException should not happen: "+e.getMessage());
} catch (FOPException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
assertNotNull(result);
// assertNotNull(result);
// Reading ZUGFeRD
assertEquals(expected, result);
// assertEquals(expected, result);
}
}

File diff suppressed because one or more lines are too long