Skip to content

Commit 69c5183

Browse files
1014821: Revamped download docs files in react
1 parent e97b6cf commit 69c5183

2 files changed

Lines changed: 193 additions & 6 deletions

File tree

Document-Processing/PDF/PDF-Viewer/react/download.md

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@ platform: document-processing
77
documentation: ug
88
domainurl: ##DomainURL##
99
---
10+
1011
# Download in React PDF Viewer component
1112

12-
The PDF Viewer component supports downloading the loaded PDF file. Enable or disable the download using the example below.
13+
The React PDF Viewer allows users to download the currently loaded PDF, including any annotations, form‑field edits, ink drawings, comments, or page reorganizations. Downloading produces a local PDF file containing all applied changes. This guide shows ways to download a PDF displayed in the PDF Viewer: using the built-in toolbar, and programmatically after editing.
14+
15+
## Download the PDF Using the Toolbar
16+
17+
The viewer's toolbar can include a download button when the `Toolbar` service is injected. When enabled, users can click the toolbar download icon to save the currently loaded PDF.
18+
19+
**Notes:**
20+
21+
- Ensure `Toolbar` is included in the `Inject` services for `PdfViewerComponent` and `DownloadOption` is included in `toolbarItems`.
22+
- See the [toolbar items documentation](./toolbar-customization/primary-toolbar#3-show-or-hide-primary-toolbar-items) for customizing or hiding the default download icon.
1323

1424
![Download button in PDF Viewer](./images/download.png)
1525

16-
Invoke the download action using the following example.
26+
## Download an Edited PDF Programmatically
1727

18-
N> The examples obtain the viewer instance and call `download()`. Prefer using the component `ref` to access the viewer instance when available rather than direct DOM lookup.
28+
You can invoke the viewer's `download()` method to trigger a download programmatically. The examples below show a standalone setup and a server-backed setup to trigger download action.
1929

2030
{% tabs %}
2131
{% highlight js tabtitle="Standalone" %}
@@ -94,7 +104,75 @@ root.render(<App />);
94104
{% endhighlight %}
95105
{% endtabs %}
96106

107+
## Download a PDF with Flattened Annotations
108+
109+
You can intercept the viewer's `downloadStart` event, cancel the default download, obtain the document as a `Blob` via `saveAsBlob()`, and flatten annotations before saving the resulting PDF.
110+
111+
{% tabs %}
112+
{% highlight ts tabtitle="App.tsx" %}
113+
{% raw %}
114+
import {
115+
PdfViewerComponent, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation,
116+
ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner,
117+
PageOrganizer, Inject, Print, DownloadStartEventArgs
118+
} from '@syncfusion/ej2-react-pdfviewer';
119+
import { PdfDocument } from '@syncfusion/ej2-pdf';
120+
import { RefObject, useRef } from 'react';
121+
122+
export default function App() {
123+
const viewerRef: RefObject<PdfViewerComponent> = useRef(null);
124+
125+
const blobToBase64 = async (blob: Blob): Promise<string> => {
126+
return new Promise((resolve, reject) => {
127+
const reader = new FileReader();
128+
reader.onerror = () => reject(reader.error);
129+
reader.onload = () => {
130+
const dataUrl: string = reader.result as string;
131+
const data: string = dataUrl.split(',')[1];
132+
resolve(data);
133+
};
134+
reader.readAsDataURL(blob);
135+
});
136+
}
137+
138+
const flattenPDF = (data: string) => {
139+
let document: PdfDocument = new PdfDocument(data);
140+
document.flatten = true
141+
document.save(`${viewerRef.current.fileName}.pdf`);
142+
document.destroy();
143+
}
144+
145+
const handleFlattening = async () => {
146+
const blob: Blob = await viewerRef.current.saveAsBlob();
147+
const data: string = await blobToBase64(blob);
148+
flattenPDF(data);
149+
}
150+
151+
const onDownloadStart = async (args: DownloadStartEventArgs) => {
152+
args.cancel = true;
153+
handleFlattening();
154+
};
155+
156+
return (
157+
<div style={{ height: '640px' }}>
158+
<PdfViewerComponent
159+
id="pdf-viewer"
160+
ref={viewerRef}
161+
documentPath="https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"
162+
resourceUrl="https://cdn.syncfusion.com/ej2/32.2.5/dist/ej2-pdfviewer-lib"
163+
downloadStart={onDownloadStart}
164+
>
165+
<Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView,
166+
BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer, Print]} />
167+
</PdfViewerComponent>
168+
</div>
169+
);
170+
}
171+
{% endraw %}
172+
{% endhighlight %}
173+
{% endtabs %}
174+
97175
## See also
98176

99-
* [Toolbar items](./toolbar)
100-
* [Feature Modules](./feature-module)
177+
- [Toolbar items](./toolbar)
178+
- [Feature Modules](./feature-module)

Document-Processing/PDF/PDF-Viewer/react/save-pdf-files.md

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,118 @@ Each link below goes to a provider page with simple, step-by-step instructions a
253253
- [Box](./save-pdf-file/to-box-cloud-file-storage)
254254
- [Azure AD (auth notes)](./save-pdf-file/to-azure-active-directory)
255255

256+
## Export PDF with or without annotations
257+
258+
The React PDF Viewer allows exporting the current PDF along with annotations, or exporting a clean version without annotations. This gives flexibility depending on workflow review mode, sharing, or securing final documents.
259+
260+
### Export PDF with annotations
261+
262+
The PDF is exported with annotations and form fields by default. You can download a PDF using the download button in the built‑in toolbar or through the `download` API. See the [download guide](./download) for further explanation.
263+
264+
### Export PDF without annotations
265+
266+
The PDF can be exported without annotations by setting the `skipDownload` API to true in `annotationSettings`.
267+
268+
{% highlight ts %}
269+
{% raw %}
270+
<PdfViewerComponent
271+
id="pdfViewer"
272+
documentPath="https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"
273+
resourceUrl="https://cdn.syncfusion.com/ej2/32.2.5/dist/ej2-pdfviewer-lib"
274+
annotationSettings={{
275+
skipDownload: true
276+
}}
277+
>
278+
<Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView,
279+
BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer, Print]} />
280+
</PdfViewerComponent>
281+
{% endraw %}
282+
{% endhighlight %}
283+
284+
### Export after restoring annotations
285+
286+
PDFs can also be exported after importing annotations. The following code example uses the `downloadStart` event to cancel the download action and import the annotations from a JSON file before downloading the PDF document.
287+
288+
{% tabs %}
289+
{% highlight ts tabtitle="App.tsx" %}
290+
{% raw %}
291+
import {
292+
PdfViewerComponent, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation,
293+
ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner,
294+
PageOrganizer, Inject, Print, DownloadStartEventArgs
295+
} from '@syncfusion/ej2-react-pdfviewer';
296+
import { DataFormat, PdfDocument } from '@syncfusion/ej2-pdf';
297+
import { RefObject, useRef } from 'react';
298+
299+
export default function App() {
300+
const viewerRef: RefObject<PdfViewerComponent> = useRef(null);
301+
302+
const blobToBase64 = async (blob: Blob): Promise<string> => {
303+
return new Promise((resolve, reject) => {
304+
const reader = new FileReader();
305+
reader.onerror = () => reject(reader.error);
306+
reader.onload = () => {
307+
const dataUrl: string = reader.result as string;
308+
const data: string = dataUrl.split(',')[1];
309+
resolve(data);
310+
};
311+
reader.readAsDataURL(blob);
312+
});
313+
}
314+
315+
const restoreAnnotations = async (data: string) => {
316+
try {
317+
const fetchResponse: Response = await fetch("/pdf-succinctly.json", { headers: { Accept: "application/json" } });
318+
if (fetchResponse.ok) {
319+
let document: PdfDocument = new PdfDocument(data);
320+
let jsonData = await fetchResponse.json();
321+
document.importAnnotations(btoa(JSON.stringify(jsonData)), DataFormat.json);
322+
document.save(`${viewerRef.current.fileName}.pdf`);
323+
document.destroy();
324+
}
325+
else {
326+
throw new Error(`HTTP ${fetchResponse.status}`);
327+
}
328+
}
329+
catch (error) {
330+
console.error(error);
331+
}
332+
}
333+
334+
const handleImports = async () => {
335+
const blob: Blob = await viewerRef.current.saveAsBlob();
336+
const data: string = await blobToBase64(blob);
337+
await restoreAnnotations(data);
338+
}
339+
340+
const onDownloadStart = async (args: DownloadStartEventArgs) => {
341+
args.cancel = true;
342+
await handleImports();
343+
};
344+
345+
return (
346+
<div style={{ height: '640px' }}>
347+
<PdfViewerComponent
348+
id="pdf-viewer"
349+
ref={viewerRef}
350+
documentPath="https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf"
351+
resourceUrl="https://cdn.syncfusion.com/ej2/32.2.5/dist/ej2-pdfviewer-lib"
352+
downloadStart={onDownloadStart}
353+
>
354+
<Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView,
355+
BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer, Print]} />
356+
</PdfViewerComponent>
357+
</div>
358+
);
359+
}
360+
{% endraw %}
361+
{% endhighlight %}
362+
{% endtabs %}
363+
256364
---
257365

258366
**See also**
259367

260368
- [Get Base64 value from a loaded PDF using saveAsBlob API](./how-to/get-base64)
261-
- [Open PDF files overview](./open-pdf-files)
369+
- [Open PDF files overview](./open-pdf-files)
370+
- [Download a PDF](./download)

0 commit comments

Comments
 (0)