mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-25 09:38:06 +02:00
Save form data in XFA datasets when pdf is a mix of acroforms and xfa (#12344)
* Move display/xml_parser.js in shared to use it in worker * Save form data in XFA datasets when pdf is a mix of acroforms and xfa Co-authored-by: Brendan Dahl <brendan.dahl@gmail.com>
This commit is contained in:
parent
622e2fbd3a
commit
68b99c59ee
11 changed files with 416 additions and 19 deletions
|
@ -14,8 +14,14 @@
|
|||
*/
|
||||
/* eslint no-var: error */
|
||||
|
||||
import { bytesToString, escapeString } from "../shared/util.js";
|
||||
import {
|
||||
bytesToString,
|
||||
escapeString,
|
||||
parseXFAPath,
|
||||
warn,
|
||||
} from "../shared/util.js";
|
||||
import { Dict, isDict, isName, isRef, isStream, Name } from "./primitives.js";
|
||||
import { SimpleDOMNode, SimpleXMLParser } from "../shared/xml_parser.js";
|
||||
import { calculateMD5 } from "./crypto.js";
|
||||
|
||||
function writeDict(dict, buffer, transform) {
|
||||
|
@ -123,7 +129,55 @@ function computeMD5(filesize, xrefInfo) {
|
|||
return bytesToString(calculateMD5(array));
|
||||
}
|
||||
|
||||
function incrementalUpdate(originalData, xrefInfo, newRefs) {
|
||||
function updateXFA(datasetsRef, newRefs, xref) {
|
||||
if (datasetsRef === null || xref === null) {
|
||||
return;
|
||||
}
|
||||
const datasets = xref.fetchIfRef(datasetsRef);
|
||||
const str = bytesToString(datasets.getBytes());
|
||||
const xml = new SimpleXMLParser(/* hasAttributes */ true).parseFromString(
|
||||
str
|
||||
);
|
||||
|
||||
for (const { xfa } of newRefs) {
|
||||
if (!xfa) {
|
||||
continue;
|
||||
}
|
||||
const { path, value } = xfa;
|
||||
if (!path) {
|
||||
continue;
|
||||
}
|
||||
const node = xml.documentElement.searchNode(parseXFAPath(path), 0);
|
||||
if (node) {
|
||||
node.childNodes = [new SimpleDOMNode("#text", value)];
|
||||
} else {
|
||||
warn(`Node not found for path: ${path}`);
|
||||
}
|
||||
}
|
||||
const buffer = [];
|
||||
xml.documentElement.dump(buffer);
|
||||
let updatedXml = buffer.join("");
|
||||
|
||||
const encrypt = xref.encrypt;
|
||||
if (encrypt) {
|
||||
const transform = encrypt.createCipherTransform(
|
||||
datasetsRef.num,
|
||||
datasetsRef.gen
|
||||
);
|
||||
updatedXml = transform.encryptString(updatedXml);
|
||||
}
|
||||
const data =
|
||||
`${datasetsRef.num} ${datasetsRef.gen} obj\n` +
|
||||
`<< /Type /EmbeddedFile /Length ${updatedXml.length}>>\nstream\n` +
|
||||
updatedXml +
|
||||
"\nendstream\nendobj\n";
|
||||
|
||||
newRefs.push({ ref: datasetsRef, data });
|
||||
}
|
||||
|
||||
function incrementalUpdate(originalData, xrefInfo, newRefs, xref, datasetsRef) {
|
||||
updateXFA(datasetsRef, newRefs, xref);
|
||||
|
||||
const newXref = new Dict(null);
|
||||
const refForXrefTable = xrefInfo.newRef;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue