Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make embedded timestamps reproducible and use SOURCE_DATE_EPOCH if available #87

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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 Down Expand Up @@ -234,6 +235,12 @@ public boolean parse(String[] args)
//Make sure the prepared serializer is used
foUserAgent.setDocumentHandlerOverride(serializer);
}

String sourceDateEpoch = System.getenv("SOURCE_DATE_EPOCH");
if (sourceDateEpoch != null) {
foUserAgent.setCreationDate(new Date(1000 * Long.parseLong(sourceDateEpoch)));
}

return true;
}

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 = this.document.getInfo().getCreationDate();
if (date == null) {
date = new Date();
}
digest.update(PDFDocument.encode(df.format(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
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ 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 date = userAgent.getCreationDate();
if (date == null) {
date = new Date();
}
xmpBasic.setMetadataDate(date);
PDFMetadata.updateInfoFromMetadata(docXMP, pdfDoc.getInfo());

PDFMetadata pdfMetadata = pdfDoc.getFactory().makeMetadata(
Expand Down