Merge pull request #14067 from calixteman/1732344

Don't save anything in XFA entry if no XFA! (bug 1732344)
This commit is contained in:
Brendan Dahl 2021-09-23 15:07:00 -07:00 committed by GitHub
commit d370a281c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 32 deletions

View File

@ -624,18 +624,18 @@ class WorkerMessageHandler {
}
const xfa = (acroForm instanceof Dict && acroForm.get("XFA")) || null;
let xfaDatasets = null;
let hasDatasets = false;
let xfaDatasetsRef = null;
let hasXfaDatasetsEntry = false;
if (Array.isArray(xfa)) {
for (let i = 0, ii = xfa.length; i < ii; i += 2) {
if (xfa[i] === "datasets") {
xfaDatasets = xfa[i + 1];
xfaDatasetsRef = xfa[i + 1];
acroFormRef = null;
hasDatasets = true;
hasXfaDatasetsEntry = true;
}
}
if (xfaDatasets === null) {
xfaDatasets = xref.getNewRef();
if (xfaDatasetsRef === null) {
xfaDatasetsRef = xref.getNewRef();
}
} else if (xfa) {
acroFormRef = null;
@ -674,8 +674,9 @@ class WorkerMessageHandler {
xrefInfo: newXrefInfo,
newRefs,
xref,
datasetsRef: xfaDatasets,
hasDatasets,
hasXfa: !!xfa,
xfaDatasetsRef,
hasXfaDatasetsEntry,
acroFormRef,
acroForm,
xfaData,

View File

@ -154,8 +154,8 @@ function writeXFADataForAcroform(str, newRefs) {
function updateXFA({
xfaData,
datasetsRef,
hasDatasets,
xfaDatasetsRef,
hasXfaDatasetsEntry,
acroFormRef,
acroForm,
newRefs,
@ -166,7 +166,7 @@ function updateXFA({
return;
}
if (!hasDatasets) {
if (!hasXfaDatasetsEntry) {
if (!acroFormRef) {
warn("XFA - Cannot save it");
return;
@ -178,7 +178,7 @@ function updateXFA({
const oldXfa = acroForm.get("XFA");
const newXfa = oldXfa.slice();
newXfa.splice(2, 0, "datasets");
newXfa.splice(3, 0, datasetsRef);
newXfa.splice(3, 0, xfaDatasetsRef);
acroForm.set("XFA", newXfa);
@ -201,25 +201,25 @@ function updateXFA({
}
if (xfaData === null) {
const datasets = xref.fetchIfRef(datasetsRef);
const datasets = xref.fetchIfRef(xfaDatasetsRef);
xfaData = writeXFADataForAcroform(datasets.getString(), newRefs);
}
const encrypt = xref.encrypt;
if (encrypt) {
const transform = encrypt.createCipherTransform(
datasetsRef.num,
datasetsRef.gen
xfaDatasetsRef.num,
xfaDatasetsRef.gen
);
xfaData = transform.encryptString(xfaData);
}
const data =
`${datasetsRef.num} ${datasetsRef.gen} obj\n` +
`${xfaDatasetsRef.num} ${xfaDatasetsRef.gen} obj\n` +
`<< /Type /EmbeddedFile /Length ${xfaData.length}>>\nstream\n` +
xfaData +
"\nendstream\nendobj\n";
newRefs.push({ ref: datasetsRef, data });
newRefs.push({ ref: xfaDatasetsRef, data });
}
function incrementalUpdate({
@ -227,22 +227,25 @@ function incrementalUpdate({
xrefInfo,
newRefs,
xref = null,
datasetsRef = null,
hasDatasets = false,
hasXfa = false,
xfaDatasetsRef = null,
hasXfaDatasetsEntry = false,
acroFormRef = null,
acroForm = null,
xfaData = null,
}) {
updateXFA({
xfaData,
datasetsRef,
hasDatasets,
acroFormRef,
acroForm,
newRefs,
xref,
xrefInfo,
});
if (hasXfa) {
updateXFA({
xfaData,
xfaDatasetsRef,
hasXfaDatasetsEntry,
acroFormRef,
acroForm,
newRefs,
xref,
xrefInfo,
});
}
const newXref = new Dict(null);
const refForXrefTable = xrefInfo.newRef;

View File

@ -1283,6 +1283,28 @@ describe("api", function () {
await Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]);
});
it("write a value in an annotation, save the pdf and load it", async function () {
let loadingTask = getDocument(buildGetDocumentParams("evaljs.pdf"));
let pdfDoc = await loadingTask.promise;
const value = "Hello World";
pdfDoc.annotationStorage.setValue("55R", { value });
const data = await pdfDoc.saveDocument();
await loadingTask.destroy();
loadingTask = getDocument(data);
pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(1);
const annotations = await pdfPage.getAnnotations();
const field = annotations.find(annotation => annotation.id === "55R");
expect(!!field).toEqual(true);
expect(field.fieldValue).toEqual(value);
await loadingTask.destroy();
});
describe("Cross-origin", function () {
let loadingTask;
function _checkCanLoad(expectSuccess, filename, options) {

View File

@ -162,7 +162,7 @@ describe("Writer", function () {
Ref.get(456, 0),
]);
const acroFormRef = Ref.get(789, 0);
const datasetsRef = Ref.get(101112, 0);
const xfaDatasetsRef = Ref.get(101112, 0);
const xfaData = "<hello>world</hello>";
const xrefInfo = {
@ -180,8 +180,9 @@ describe("Writer", function () {
originalData,
xrefInfo,
newRefs,
datasetsRef,
hasDatasets: false,
hasXfa: true,
xfaDatasetsRef,
hasXfaDatasetsEntry: false,
acroFormRef,
acroForm,
xfaData,

View File

@ -1015,6 +1015,7 @@ const PDFViewerApplication = {
} catch (reason) {
// When the PDF document isn't ready, or the PDF file is still
// downloading, simply fallback to a "regular" download.
console.error(`Error when saving the document: ${reason.message}`);
await this.download({ sourceEventType });
} finally {
await this.pdfScriptingManager.dispatchDidSave();