Save PDF files to Azure Blob Storage in Vue
17 Nov 202510 minutes to read
The Vue PDF Viewer component supports saving PDF files to Azure Blob Storage using either the standalone or server-backed configuration. The following steps demonstrate both approaches..
Using Standalone PDF Viewer
To save a PDF file to Azure Blob Storage, follow these steps:
Step 1: Create a PDF Viewer sample in Vue
Follow the instructions provided in this link to create a simple PDF Viewer sample in Vue. This sets up the basic structure of the PDF Viewer application.
Step 2: Modify the src/app/app.ts file in the Angular project
- Import the required namespaces at the top of the file:
<script>
import { BlockBlobClient } from "@azure/storage-blob";
</script>- Add the following private property to
app.ts, and assign the value from the configuration to the corresponding property.
<template>
<ejs-pdfviewer
id="pdfViewer"
:resourceUrl="resourceUrl"
:toolbarClick="toolbarClick"
:created="loadPdfDocument"
:toolbarSettings="toolbarSettings">
</ejs-pdfviewer>
</template>
<script>
export default {
data() {
let toolItem1 = {
prefixIcon: 'e-icons e-pv-download-document-icon',
id: 'download_pdf',
tooltipText: 'Download file',
align: 'right'
};
return {
resourceUrl: 'https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib',
toolbarSettings: {
toolbarItems: [ 'OpenOption', 'PageNavigationTool', 'MagnificationTool', 'PanTool', 'SelectionTool', 'SearchOption', 'PrintOption', toolItem1, 'UndoRedoTool', 'AnnotationEditTool', 'FormDesignerEditTool', 'CommentTool', 'SubmitForm']
},
};
},
methods: {
toolbarClick: function (args) {
if (args.item && args.item.id === 'download_pdf') {
this.savePdfDocument();
}
},
}
}
</script>- 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.
NOTE
Replace Your SAS Url in Azure with the actual SAS URL for the Azure Blob Storage account.
<script>
export default {
methods: {
savePdfDocument: function () {
var viewer = document.getElementById('pdfViewer').ej2_instances[0];
viewer.saveAsBlob().then(function (value) {
var reader = new FileReader();
reader.onload = async () => {
if (reader.result) {
var SASUrl = "*Your SAS Url in Azure*";
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);
});
}
}
}
</script>NOTE
Install the @azure/storage-blob package in the application to use the previous code example: npm install @azure/storage-blob
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 Vue
Follow the instructions provided in this link to create a simple PDF Viewer sample in Vue. This will set up the basic structure of your 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 Vue 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.
<template>
<div id="app">
<ejs-pdfviewer id="pdfViewer" :serviceUrl="serviceUrl" :documentPath="documentPath">
</ejs-pdfviewer>
</div>
</template>
<script setup>
import { provide } from "vue";
import {
PdfViewerComponent as EjsPdfviewer, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner
} from '@syncfusion/ej2-vue-pdfviewer';
// Replace the "localhost:44396" with the actual URL of your server
const serviceUrl = "https://localhost:44396/pdfviewer";
const documentPath = "PDF_Succinctly.pdf";
provide('PdfViewer', [Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner]);
</script><template>
<div id="app">
<ejs-pdfviewer id="pdfViewer" :serviceUrl="serviceUrl" :documentPath="documentPath">
</ejs-pdfviewer>
</div>
</template>
<script>
import {
PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner
} from '@syncfusion/ej2-vue-pdfviewer';
export default {
name: 'app',
components: {
'ejs-pdfviewer': PdfViewerComponent
},
data() {
return {
// Replace the "localhost:44396" with the actual URL of your server
serviceUrl: "https://localhost:44396/pdfviewer",
documentPath: "PDF_Succinctly.pdf"
};
},
provide: {
PdfViewer: [Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, TextSearch, Annotation, FormFields, FormDesigner]
}
}
</script>NOTE
Install the Azure.Storage.Blobs NuGet package in the web service application to use the previous code example.