Skip to content

Save document

In Foxit PDF SDK for Web, “saving a document” usually means exporting the current PDFDoc to a new PDF file stream, then downloading it locally, uploading it to a server, or reopening it in the Viewer—depending on your app.

The common API is PDFDoc.getFile(). It exports the current PDF document object as a File, including changes already written to PDFDoc (annotations, forms, page edits, and so on).

NOTE

Web SDK runs in the browser and cannot write to arbitrary paths on the user’s disk. Saving locally typically uses the browser download flow; saving to your system usually uploads the exported file to a server.

Get the current document

With UIExtension, get PDFViewer via pdfui.getPDFViewer(), then getCurrentPDFDoc():

javascript
const pdfViewer = await pdfui.getPDFViewer();
const pdfDoc = pdfViewer.getCurrentPDFDoc();

if (!pdfDoc) {
    throw new Error('No document is open');
}

If you use PDFViewer directly:

javascript
const pdfDoc = pdfViewer.getCurrentPDFDoc();

if (!pdfDoc) {
    throw new Error('No document is open');
}

Export as a PDF file

PDFDoc.getFile() returns a Promise<File>.

javascript
const file = await pdfDoc.getFile({
    fileName: 'saved.pdf'
});

Common parameters:

ParameterTypeDescription
fileNamestringExported PDF file name. Defaults to the current document name if omitted.
flagsnumberSave flags. Default 0 is a normal save.
progressHandlerFunctionExport progress callback with current, total, and status.

Download locally

In the browser, trigger download with URL.createObjectURL() and an <a> element:

javascript
async function downloadCurrentPDF(pdfui) {
    const pdfViewer = await pdfui.getPDFViewer();
    const pdfDoc = pdfViewer.getCurrentPDFDoc();

    if (!pdfDoc) {
        throw new Error('No document is open');
    }

    const file = await pdfDoc.getFile({
        fileName: 'saved.pdf'
    });

    const url = URL.createObjectURL(file);
    const a = document.createElement('a');
    a.href = url;
    a.download = file.name || 'saved.pdf';
    a.click();
    URL.revokeObjectURL(url);
}

Save to a server

Upload the file returned by getFile() with FormData:

javascript
async function uploadCurrentPDF(pdfui) {
    const pdfViewer = await pdfui.getPDFViewer();
    const pdfDoc = pdfViewer.getCurrentPDFDoc();

    if (!pdfDoc) {
        throw new Error('No document is open');
    }

    const file = await pdfDoc.getFile({
        fileName: 'saved.pdf'
    });

    const formData = new FormData();
    formData.append('file', file, file.name);

    await fetch('/api/pdf/save', {
        method: 'POST',
        body: formData
    });
}

Export progress

getFile() accepts progressHandler for export progress—useful for progress bars or disabling the save button.

javascript
const file = await pdfDoc.getFile({
    fileName: 'saved.pdf',
    progressHandler: function({ current, total, status }) {
        console.log('Save progress:', current, total, status);
    }
});

For progress UI, see Progress component.

Save flags

The flags parameter on getFile() controls how the document is saved. Default 0 is a normal save.

Common flags:

FlagValueDescription
normal0Normal save. Use this flag alone.
incremental0x0001Incremental save.
noOriginal0x0002Save without original data or unchanged objects.
XRefStream0x0008Save using an XRef stream.
linearized0x1000Save as a linearized PDF. Use this flag alone.
removeRedundantObjects0x0010Remove redundant PDF objects on save.

Example: normal save.

javascript
const file = await pdfDoc.getFile({
    flags: 0,
    fileName: 'saved.pdf'
});

If you have no special PDF save policy, use the default.

Export document stream in chunks

For custom chunked handling, use PDFDoc.getStream(). It invokes a callback with chunk data and returns the total size when done.

javascript
const buffers = [];

const size = await pdfDoc.getStream(function({ arrayBuffer, offset, size }) {
    buffers.push(arrayBuffer);
});

console.log('Exported file size:', size);

const blob = new Blob(buffers, {
    type: 'application/pdf'
});

getStream() suits advanced upload or stream processing. For typical save/download, prefer getFile().

Reopen the saved document

To reload the exported file in the Viewer after saving:

javascript
const file = await pdfDoc.getFile({
    fileName: 'saved.pdf'
});

await pdfViewer.openPDFByFile(file, {
    fileName: file.name
});

Permission restrictions

On export, the SDK checks download permissions. If the document or app disallows download, getFile() or getStream() throws a permission error.

javascript
try {
    const file = await pdfDoc.getFile({
        fileName: 'saved.pdf'
    });
} catch (error) {
    console.error('Failed to save document', error);
}

Notes

  • getFile() returns a new file object; it does not trigger download or upload automatically.
  • The browser cannot overwrite the user’s original local file; your app implements download or upload.
  • If business data is synced asynchronously, ensure it is written to PDFDoc before saving.
  • Large documents may take time to export; use progressHandler to inform users.
  • To export only annotations or form data, use the corresponding export APIs—not a full PDF export.