How can I help you?
Save PDF files to Azure Blob Storage in React
25 Feb 20269 minutes to read
The React PDF Viewer component supports saving PDF files to Azure Blob Storage using either a standalone (browser) configuration or a server-backed configuration. The following steps demonstrate both approaches and include prerequisites and security guidance for production deployments.
Using Standalone PDF Viewer
Follow the steps below to save a PDF file to Azure Blob Storage from an React PDF Viewer.
Step 1: Create a PDF Viewer sample in React
Follow the instructions provided in this link to create a simple PDF Viewer sample in React. This will set up the basic structure of your PDF Viewer application.
Step 2: Modify the src/app/app.ts file in the React project
- Import the required namespaces at the top of the file:
import { BlockBlobClient } from "@azure/storage-blob";- Add the following private property to
app.ts, and assign the value from the configuration to the corresponding property.
NOTE
Replace Your SAS Url in Azure with the SAS URL generated for the target blob. For production, generate short-lived SAS tokens on the server rather than embedding SAS URLs in client code.
var SASUrl = "*Your SAS Url in Azure*";- Configure a custom toolbar item for the download function to save a PDF file to Azure Blob Storage.
var toolItem1 = {
prefixIcon: 'e-icons e-pv-download-document-icon',
id: 'download_pdf',
tooltipText: 'Download file',
align: 'right'
};
function toolbarClick(args){
if (args.item && args.item.id === 'download_pdf') {
saveDocument();
}
};
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
ref={(scope) => {
viewer = scope;
}}
created={loadDocument}
id="container"
resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib"
style={{ height: '640px' }}
toolbarSettings={{ showTooltip : true, toolbarItems: [ 'OpenOption', 'PageNavigationTool', 'MagnificationTool', 'PanTool', 'SelectionTool', 'SearchOption', 'PrintOption', toolItem1, 'UndoRedoTool', 'AnnotationEditTool', 'FormDesignerEditTool', 'CommentTool', 'SubmitForm']}}
toolbarClick={toolbarClick}
>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, FormFields, FormDesigner ]}/>
</PdfViewerComponent>
</div>
</div>);- Retrieve the PDF Viewer instance and save the current PDF as a Blob. Then, read the Blob as an ArrayBuffer and upload the ArrayBuffer to Azure Blob Storage using
BlockBlobClient.
function saveDocument() {
viewer.saveAsBlob().then(function (value) {
var reader = new FileReader();
reader.onload = async () => {
if (reader.result) {
const arrayBuffer = reader.result;
const blobClient = new BlockBlobClient(SASUrl);
// Upload data to the blob
const uploadBlobResponse = await blobClient.upload(arrayBuffer, arrayBuffer.byteLength);
console.log(`Upload blob successfully`, uploadBlobResponse.requestId);
}
};
reader.readAsArrayBuffer(value);
});
}NOTE
Install the Azure Storage Blob client package for browser use:
npm install @azure/storage-blob. For server-side operations usedotnet add package Azure.Storage.Blobs.
Using server-backed PDF Viewer
To save a PDF file to Azure Blob Storage, you can follow the steps below:
Step 1: Create a PDF Viewer sample in React
Follow the instructions provided in this link to create a simple PDF Viewer sample in React. This sets up the basic structure of the PDF Viewer application.
Step 2: Modify the PdfViewerController.cs file in the web service project
-
Create a web service project in .NET Core 3.0 or later. For instructions, see this article: https://www.syncfusion.com/kb/11063/how-to-create-pdf-viewer-web-service-in-net-core-3-0-and-above
-
Open the
PdfViewerController.csfile in your web service project. -
Import the required namespaces at the top of the file:
using System.IO;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Specialized;- Add the following private fields and constructor parameters to the
PdfViewerControllerclass, In the constructor, assign the values from the configuration to the corresponding fields
private readonly string _storageConnectionString;
private readonly string _storageContainerName;
private readonly ILogger<PdfViewerController> _logger;
public PdfViewerController(IConfiguration configuration, ILogger<PdfViewerController> logger)
{
_storageConnectionString = configuration.GetValue<string>("connectionString");
_storageContainerName = configuration.GetValue<string>("containerName");
_logger = logger;
}- Modify the Download() method to save the downloaded PDF file to the Azure Blob Storage container.
[HttpPost("Download")]
[Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]
[Route("[controller]/Download")]
//Post action for downloading the PDF documents
public IActionResult Download([FromBody] Dictionary<string, string> jsonObject)
{
// Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
string document = jsonObject["documentId"];
BlobServiceClient blobServiceClient = new BlobServiceClient(_storageConnectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(_storageContainerName);
string result = Path.GetFileNameWithoutExtension(document);
// Get a reference to the blob
BlobClient blobClient = containerClient.GetBlobClient(result + "_downloaded.pdf");
// Convert the document base64 string to bytes
byte[] bytes = Convert.FromBase64String(documentBase.Split(",")[1]);
// Upload the document to Azure Blob Storage
using (MemoryStream stream = new MemoryStream(bytes))
{
blobClient.Upload(stream, true);
}
return Content(documentBase);
}- Open the
appsettings.jsonfile in the web service project and add the following lines below the existing"AllowedHosts"configuration.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"connectionString": "*Your Connection string from Azure*",
"containerName": "*Your container name in Azure*"
}NOTE
Replace the placeholders with the actual Azure Storage connection string and container name.
Step 3: Set the PDF Viewer properties in the React PDF Viewer component
Modify the serviceUrl property of the PDF Viewer component with the accurate URL of the web service, replacing https://localhost:44396/pdfviewer with the actual server URL. Set the documentPath property to the desired PDF file name to load from Azure Blob Storage, and ensure that the document exists in the target container.
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, Annotation, TextSearch, Inject, FormDesigner, FormFields} from '@syncfusion/ej2-react-pdfviewer';
function App() {
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="PDF_Succinctly.pdf"
// Replace the "localhost:44396" with the actual URL of your server
serviceUrl="https://localhost:44396/pdfviewer"
style={{ 'height': '640px' }}>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, FormDesigner, FormFields ]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);NOTE
Install the Azure.Storage.Blobs NuGet package in the web service application to use the previous code example.