Skip to content

Commit

Permalink
Merge pull request #16941 from Snuffleupagus/SaveDocument-await-reque…
Browse files Browse the repository at this point in the history
…stLoadedStream

Ensure that the entire PDF document is loaded *before* we begin saving it
  • Loading branch information
Snuffleupagus authored Sep 12, 2023
2 parents 953f271 + 50937a3 commit b157822
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,19 +535,21 @@ class WorkerMessageHandler {
handler.on(
"SaveDocument",
async function ({ isPureXfa, numPages, annotationStorage, filename }) {
const promises = [
const globalPromises = [
pdfManager.requestLoadedStream(),
pdfManager.ensureCatalog("acroForm"),
pdfManager.ensureCatalog("acroFormRef"),
pdfManager.ensureDoc("startXRef"),
pdfManager.ensureDoc("xref"),
pdfManager.ensureDoc("linearization"),
];
const promises = [];

const newAnnotationsByPage = !isPureXfa
? getNewAnnotationsMap(annotationStorage)
: null;

const xref = await pdfManager.ensureDoc("xref");
const [stream, acroForm, acroFormRef, startXRef, xref, linearization] =
await Promise.all(globalPromises);

if (newAnnotationsByPage) {
const imagePromises = AnnotationFactory.generateImages(
Expand Down Expand Up @@ -587,14 +589,7 @@ class WorkerMessageHandler {
}
}

return Promise.all(promises).then(function ([
stream,
acroForm,
acroFormRef,
startXRef,
linearization,
...refs
]) {
return Promise.all(promises).then(refs => {
let newRefs = [];
let xfaData = null;
if (isPureXfa) {
Expand Down

0 comments on commit b157822

Please sign in to comment.