diff --git a/fop-core/src/main/java/org/apache/fop/cli/CommandLineOptions.java b/fop-core/src/main/java/org/apache/fop/cli/CommandLineOptions.java index 8d824b96055..c2c177e473e 100644 --- a/fop-core/src/main/java/org/apache/fop/cli/CommandLineOptions.java +++ b/fop-core/src/main/java/org/apache/fop/cli/CommandLineOptions.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.PrintStream; import java.net.URI; +import java.util.Date; import java.util.Locale; import java.util.Map; import java.util.Vector; @@ -36,6 +37,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.xmlgraphics.util.DateFormatUtil; + import org.apache.fop.Version; import org.apache.fop.accessibility.Accessibility; import org.apache.fop.apps.FOPException; @@ -55,6 +58,7 @@ import org.apache.fop.render.intermediate.IFDocumentHandler; import org.apache.fop.render.intermediate.IFSerializer; import org.apache.fop.render.pdf.PDFEncryptionOption; +import org.apache.fop.render.pdf.PDFRendererOption; import org.apache.fop.render.print.PagesMode; import org.apache.fop.render.print.PrintRenderer; import org.apache.fop.render.xml.XMLRenderer; @@ -128,6 +132,8 @@ public class CommandLineOptions { private boolean useComplexScriptFeatures = true; /* set to true if -dpi used in command line */ private boolean overrideTargetResolution; + /* creation date set with -creationdate, or null */ + private Date creationDate; private FopFactory factory; private FOUserAgent foUserAgent; @@ -182,6 +188,9 @@ public boolean parse(String[] args) addXSLTParameter("fop-output-format", getOutputFormat()); addXSLTParameter("fop-version", Version.getVersion()); foUserAgent.setConserveMemoryPolicy(conserveMemoryPolicy); + if (creationDate != null) { + foUserAgent.setCreationDate(creationDate); + } // TODO: Handle this!! //if (!useComplexScriptFeatures) { // foUserAgent.setComplexScriptFeaturesEnabled(false); @@ -376,6 +385,8 @@ private boolean parseOptions(String[] args) throws FOPException { i = i + parsePDFUserPassword(args, i); } else if (args[i].equals("-pdfprofile")) { i = i + parsePDFProfile(args, i); + } else if (args[i].equals("-creationdate")) { + i = i + parseCreationDate(args, i); } else if (args[i].equals("-noprint")) { getPDFEncryptionParams().setAllowPrint(false); } else if (args[i].equals("-nocopy")) { @@ -443,6 +454,21 @@ private int parseLanguageOption(String[] args, int i) throws FOPException { } } + private int parseCreationDate(String[] args, int i) throws FOPException { + if ((i + 1 == args.length) + || (isOption(args[i + 1]))) { + throw new FOPException("if you use '-creationdate', you must specify a date" + + " in ISO 8601 format, for example 2026-11-01T00:00:00Z"); + } + try { + creationDate = DateFormatUtil.parseISO8601Date(args[i + 1]); + } catch (IllegalArgumentException e) { + throw new FOPException(e.getMessage()); + } + renderingOptions.put(PDFRendererOption.FILE_ID_FROM_CREATION_DATE.getName(), Boolean.TRUE); + return 1; + } + private int parseResolution(String[] args, int i) throws FOPException { if ((i + 1 == args.length) || (isOption(args[i + 1]))) { @@ -1216,7 +1242,11 @@ public static void printUsage(PrintStream out) { + " print high quality permission\n" + " -a enables accessibility features (Tagged PDF etc., default off)\n" + " -pdfprofile prof PDF file will be generated with the specified profile\n" - + " (Examples for prof: PDF/A-1b or PDF/X-3:2003)\n\n" + + " (Examples for prof: PDF/A-1b or PDF/X-3:2003)\n" + + " -creationdate date use date (ISO 8601, for example 2026-11-01T00:00:00Z)" + + " for the document\n" + + " dates and the PDF file ID, so the same input gives" + + " the same file\n\n" + " -conserve enable memory-conservation policy (trades memory-consumption" + " for disk I/O)\n" + " (Note: currently only influences whether the area tree is" diff --git a/fop-core/src/main/java/org/apache/fop/pdf/FileIDGenerator.java b/fop-core/src/main/java/org/apache/fop/pdf/FileIDGenerator.java index 00aad442657..e6d4426a90e 100644 --- a/fop-core/src/main/java/org/apache/fop/pdf/FileIDGenerator.java +++ b/fop-core/src/main/java/org/apache/fop/pdf/FileIDGenerator.java @@ -86,7 +86,11 @@ byte[] getUpdatedFileID() { private void generateFileID() { DateFormat df = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS"); - digest.update(PDFDocument.encode(df.format(new Date()))); + Date date = null; + if (document.isFileIDFromCreationDate()) { + date = document.getInfo().getCreationDate(); + } + digest.update(PDFDocument.encode(df.format(date != null ? date : new Date()))); // Ignoring the filename here for simplicity even though it's recommended // by the PDF spec digest.update(PDFDocument.encode(String.valueOf(document.getCurrentFileSize()))); diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java index 2fac869899f..636113910b1 100644 --- a/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java +++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java @@ -193,6 +193,8 @@ public class PDFDocument { private boolean forceUriBasicLink; + private boolean fileIDFromCreationDate; + /** * Creates an empty PDF document. * @@ -1345,4 +1347,17 @@ public boolean isForceUriBasicLink() { public void setForceUriBasicLink(boolean forceUriBasicLink) { this.forceUriBasicLink = forceUriBasicLink; } + + public boolean isFileIDFromCreationDate() { + return fileIDFromCreationDate; + } + + /** + * Computes the file ID from the creation date in the document information dictionary + * instead of the current time, so that the same input and creation date give the same file. + * @param fileIDFromCreationDate true to use the creation date + */ + public void setFileIDFromCreationDate(boolean fileIDFromCreationDate) { + this.fileIDFromCreationDate = fileIDFromCreationDate; + } } diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFEmbeddedFile.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFEmbeddedFile.java index 9b444cb2428..e4a779fd40e 100644 --- a/fop-core/src/main/java/org/apache/fop/pdf/PDFEmbeddedFile.java +++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFEmbeddedFile.java @@ -31,12 +31,21 @@ public class PDFEmbeddedFile extends PDFStream { * Creates a new embedded file stream. */ public PDFEmbeddedFile() { + this(null); + } + + /** + * Creates a new embedded file stream. + * @param date the date for its creation and modification dates, or null for the current time + */ + public PDFEmbeddedFile(Date date) { super(); put("Type", new PDFName("EmbeddedFile")); put("Subtype", new PDFName("application/octet-stream")); PDFDictionary params = new PDFDictionary(); - params.put("CreationDate", PDFInfo.formatDateTime(new Date())); - params.put("ModDate", PDFInfo.formatDateTime(new Date())); + String formattedDate = PDFInfo.formatDateTime(date != null ? date : new Date()); + params.put("CreationDate", formattedDate); + params.put("ModDate", formattedDate); put("Params", params); } diff --git a/fop-core/src/main/java/org/apache/fop/render/intermediate/IFRenderer.java b/fop-core/src/main/java/org/apache/fop/render/intermediate/IFRenderer.java index 075f94253cf..6132c86f1cf 100644 --- a/fop-core/src/main/java/org/apache/fop/render/intermediate/IFRenderer.java +++ b/fop-core/src/main/java/org/apache/fop/render/intermediate/IFRenderer.java @@ -545,7 +545,8 @@ private Metadata createDefaultDocumentMetadata() { } else { xmpBasic.setCreatorTool(Version.getVersion()); } - xmpBasic.setMetadataDate(new java.util.Date()); + java.util.Date creationDate = getUserAgent().getCreationDate(); + xmpBasic.setMetadataDate(creationDate != null ? creationDate : new java.util.Date()); if (getUserAgent().getCreationDate() != null) { xmpBasic.setCreateDate(getUserAgent().getCreationDate()); } else { diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java index 12b1b3593a8..72451d66125 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java @@ -57,6 +57,7 @@ import static org.apache.fop.render.pdf.PDFEncryptionOption.OWNER_PASSWORD; import static org.apache.fop.render.pdf.PDFEncryptionOption.USER_PASSWORD; import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE; +import static org.apache.fop.render.pdf.PDFRendererOption.FILE_ID_FROM_CREATION_DATE; import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST; import static org.apache.fop.render.pdf.PDFRendererOption.FORCE_URI_BASIC_LINK; import static org.apache.fop.render.pdf.PDFRendererOption.FORM_XOBJECT; @@ -159,6 +160,7 @@ private void configure(Configuration cfg, FOUserAgent userAgent, boolean strict) parseAndPut(FORM_XOBJECT, cfg); parseAndPut(OBJECT_STREAMS, cfg); parseAndPut(FORCE_URI_BASIC_LINK, cfg); + parseAndPut(FILE_ID_FROM_CREATION_DATE, cfg); parseAndPut(VERSION, cfg); configureSignParams(cfg); } catch (ConfigurationException e) { diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOption.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOption.java index 466ffcb8812..9c4a748ca6b 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOption.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOption.java @@ -133,6 +133,16 @@ Object deserialize(String value) { Boolean deserialize(String value) { return Boolean.valueOf(value); } + }, + /** + * Rendering Options key for computing the file ID from the creation date set on the user + * agent instead of the current time, default: false + */ + FILE_ID_FROM_CREATION_DATE("file-id-from-creation-date", false) { + @Override + Boolean deserialize(String value) { + return Boolean.valueOf(value); + } }; private final String name; diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOptionsConfig.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOptionsConfig.java index 6c451c2f9ec..cb62dc5055d 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOptionsConfig.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOptionsConfig.java @@ -33,6 +33,7 @@ import org.apache.fop.pdf.PDFXMode; import org.apache.fop.pdf.Version; import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE; +import static org.apache.fop.render.pdf.PDFRendererOption.FILE_ID_FROM_CREATION_DATE; import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST; import static org.apache.fop.render.pdf.PDFRendererOption.FORCE_URI_BASIC_LINK; import static org.apache.fop.render.pdf.PDFRendererOption.FORM_XOBJECT; @@ -168,4 +169,8 @@ public Boolean getForceUriBasicLinkEnabled() { return (Boolean)properties.get(FORCE_URI_BASIC_LINK); } + public Boolean getFileIDFromCreationDate() { + return (Boolean)properties.get(FILE_ID_FROM_CREATION_DATE); + } + } diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java index 6590e7b3a7f..9ebf489b09a 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java @@ -262,7 +262,8 @@ public void renderXMPMetadata(XMPMetadata metadata) { fopXMP.mergeInto(docXMP, exclude); XMPBasicAdapter xmpBasic = XMPBasicSchema.getAdapter(docXMP); //Metadata was changed so update metadata date - xmpBasic.setMetadataDate(new java.util.Date()); + Date creationDate = userAgent.getCreationDate(); + xmpBasic.setMetadataDate(creationDate != null ? creationDate : new Date()); PDFMetadata.updateInfoFromMetadata(docXMP, pdfDoc.getInfo()); PDFMetadata pdfMetadata = pdfDoc.getFactory().makeMetadata( @@ -482,7 +483,9 @@ public void renderDictionaryExtension(PDFDictionaryExtension extension, PDFPage augmentDictionary((PDFDictionary)currentPage.get("DPart"), extension); } } else if (type == PDFDictionaryType.PagePiece) { - String date = DateFormatUtil.formatPDFDate(new Date(), TimeZone.getDefault()); + Date lastModified = userAgent.getCreationDate(); + String date = DateFormatUtil.formatPDFDate(lastModified != null ? lastModified : new Date(), + TimeZone.getDefault()); if (currentPage.get("PieceInfo") == null) { currentPage.put("PieceInfo", new PDFDictionary()); currentPage.put("LastModified", date); @@ -639,6 +642,7 @@ public PDFDocument setupPDFDocument(OutputStream out) throws IOException { pdfDoc.setFormXObjectEnabled(rendererConfig.getFormXObjectEnabled()); pdfDoc.setObjectStreamsEnabled(rendererConfig.getObjectStreamsEnabled()); pdfDoc.setForceUriBasicLink(rendererConfig.getForceUriBasicLinkEnabled()); + pdfDoc.setFileIDFromCreationDate(rendererConfig.getFileIDFromCreationDate()); return this.pdfDoc; } @@ -679,7 +683,7 @@ public void addEmbeddedFile(PDFEmbeddedFileAttachment embeddedFile) } //Create embedded file - PDFEmbeddedFile file = new PDFEmbeddedFile(); + PDFEmbeddedFile file = new PDFEmbeddedFile(userAgent.getCreationDate()); this.pdfDoc.registerObject(file); URI srcURI; try { diff --git a/fop-core/src/test/java/org/apache/fop/cli/CommandLineOptionsTestCase.java b/fop-core/src/test/java/org/apache/fop/cli/CommandLineOptionsTestCase.java index 9761838142c..5356e8904b4 100644 --- a/fop-core/src/test/java/org/apache/fop/cli/CommandLineOptionsTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/cli/CommandLineOptionsTestCase.java @@ -20,6 +20,7 @@ package org.apache.fop.cli; import java.io.IOException; +import java.util.Date; import org.junit.Before; import org.junit.Test; @@ -27,6 +28,7 @@ import static org.junit.Assert.assertTrue; import org.apache.fop.apps.FOPException; +import org.apache.fop.render.pdf.PDFRendererOption; public class CommandLineOptionsTestCase { @@ -69,4 +71,19 @@ public void testVandVersionSwitchs() throws FOPException, IOException { CommandLineOptions clo3 = new CommandLineOptions(); assertTrue(clo3.parse(cmd3)); } + + @Test + public void testCreationDate() throws Exception { + CommandLineOptions options = new CommandLineOptions(); + assertTrue(options.parse( + "-creationdate 2026-11-01T00:07:38Z -fo examples/fo/basic/simple.fo -print".split(" "))); + assertEquals(new Date(1000L * 1793491658L), options.getFOUserAgent().getCreationDate()); + assertEquals(Boolean.TRUE, options.getFOUserAgent().getRendererOption( + PDFRendererOption.FILE_ID_FROM_CREATION_DATE)); + } + + @Test(expected = FOPException.class) + public void testInvalidCreationDate() throws Exception { + new CommandLineOptions().parse("-creationdate 2026-11-01 -fo examples/fo/basic/simple.fo -print".split(" ")); + } } diff --git a/fop-core/src/test/java/org/apache/fop/pdf/PDFCreationDateTestCase.java b/fop-core/src/test/java/org/apache/fop/pdf/PDFCreationDateTestCase.java new file mode 100644 index 00000000000..c83b31c2cec --- /dev/null +++ b/fop-core/src/test/java/org/apache/fop/pdf/PDFCreationDateTestCase.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.pdf; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.util.Arrays; +import java.util.Date; +import java.util.TimeZone; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.xmlgraphics.util.DateFormatUtil; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; +import org.apache.fop.apps.FopFactory; +import org.apache.fop.apps.MimeConstants; + +/** + * Checks that the creation date set on the user agent is used for every date FOP writes + * and, with the file-id-from-creation-date option, for the file ID. + */ +public class PDFCreationDateTestCase { + + private static final Date CREATION_DATE = new Date(1000L * 1793491658L); + + @Test + public void testMetadataDateUsesCreationDate() throws Exception { + String pdf = new String(render(false), "ISO-8859-1"); + String date = DateFormatUtil.formatISO8601(CREATION_DATE, TimeZone.getDefault()); + Assert.assertTrue(pdf, pdf.contains("" + date + "")); + Assert.assertTrue(pdf, pdf.contains("" + date + "")); + } + + @Test + public void testEmbeddedFileUsesCreationDate() { + PDFEmbeddedFile file = new PDFEmbeddedFile(CREATION_DATE); + PDFDictionary params = (PDFDictionary) file.get("Params"); + Assert.assertEquals(PDFInfo.formatDateTime(CREATION_DATE), params.get("CreationDate")); + Assert.assertEquals(PDFInfo.formatDateTime(CREATION_DATE), params.get("ModDate")); + } + + @Test + public void testFileIDFromCreationDateGivesSameFile() throws Exception { + byte[] first = render(true); + Thread.sleep(50); + byte[] second = render(true); + Assert.assertArrayEquals(first, second); + } + + @Test + public void testFileIDUsesCurrentTimeByDefault() throws Exception { + byte[] first = render(false); + Thread.sleep(50); + byte[] second = render(false); + Assert.assertFalse(Arrays.equals(first, second)); + } + + private byte[] render(boolean fileIDFromCreationDate) throws Exception { + String fopxconf = "" + + "" + fileIDFromCreationDate + "" + + ""; + FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI(), + new ByteArrayInputStream(fopxconf.getBytes("UTF-8"))); + FOUserAgent userAgent = fopFactory.newFOUserAgent(); + userAgent.setCreationDate(CREATION_DATE); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out); + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + Source src = new StreamSource(PDFCreationDateTestCase.class.getResource("PDFPageXMP.fo").openStream()); + Result res = new SAXResult(fop.getDefaultHandler()); + transformer.transform(src, res); + return out.toByteArray(); + } +} diff --git a/fop-core/src/test/java/org/apache/fop/pdf/PDFPagePieceTestCase.java b/fop-core/src/test/java/org/apache/fop/pdf/PDFPagePieceTestCase.java index 158be2c797f..43c5fad16b9 100644 --- a/fop-core/src/test/java/org/apache/fop/pdf/PDFPagePieceTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/pdf/PDFPagePieceTestCase.java @@ -22,6 +22,8 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.util.Arrays; +import java.util.Date; +import java.util.TimeZone; import javax.xml.transform.stream.StreamResult; @@ -30,6 +32,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.apache.xmlgraphics.util.DateFormatUtil; + import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.FopFactory; import org.apache.fop.render.intermediate.IFContext; @@ -43,8 +47,22 @@ public class PDFPagePieceTestCase { @Test public void testPDF() throws IFException { + String pdf = renderPagePiece(null); + Assert.assertTrue(pdf, pdf.contains("/PieceInfo << /a << >> /LastModified (D:")); + } + + @Test + public void testLastModifiedUsesCreationDate() throws IFException { + Date creationDate = new Date(1000L * 1793491658L); + String pdf = renderPagePiece(creationDate); + String date = DateFormatUtil.formatPDFDate(creationDate, TimeZone.getDefault()); + Assert.assertTrue(pdf, pdf.contains("/PieceInfo << /a << >> /LastModified (" + date + ")")); + } + + private String renderPagePiece(Date creationDate) throws IFException { FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); FOUserAgent userAgent = fopFactory.newFOUserAgent(); + userAgent.setCreationDate(creationDate); PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(userAgent)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); documentHandler.setResult(new StreamResult(bos)); @@ -60,6 +78,6 @@ public void testPDF() throws IFException { documentHandler.handleExtensionObject(new PDFDictionaryAttachment(dictionaryExtension)); documentHandler.endPage(); - Assert.assertTrue(bos.toString(), bos.toString().contains("/PieceInfo << /a << >> /LastModified (D:")); + return bos.toString(); } }