Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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]))) {
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
Expand Down
15 changes: 15 additions & 0 deletions fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ public class PDFDocument {

private boolean forceUriBasicLink;

private boolean fileIDFromCreationDate;

/**
* Creates an empty PDF document.
*
Expand Down Expand Up @@ -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;
}
}
13 changes: 11 additions & 2 deletions fop-core/src/main/java/org/apache/fop/pdf/PDFEmbeddedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
package org.apache.fop.cli;

import java.io.IOException;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.fop.apps.FOPException;
import org.apache.fop.render.pdf.PDFRendererOption;

public class CommandLineOptionsTestCase {

Expand Down Expand Up @@ -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(" "));
}
}
102 changes: 102 additions & 0 deletions fop-core/src/test/java/org/apache/fop/pdf/PDFCreationDateTestCase.java
Original file line number Diff line number Diff line change
@@ -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("<xmp:MetadataDate>" + date + "</xmp:MetadataDate>"));
Assert.assertTrue(pdf, pdf.contains("<xmp:CreateDate>" + date + "</xmp:CreateDate>"));
}

@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 = "<fop version=\"1.0\"><renderers><renderer mime=\"application/pdf\">"
+ "<file-id-from-creation-date>" + fileIDFromCreationDate + "</file-id-from-creation-date>"
+ "</renderer></renderers></fop>";
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();
}
}
Loading