How can I help you?
Open PDF from Google Cloud Storage in Vue
28 Feb 20266 minutes to read
Follow these steps to load a PDF from Google Cloud Storage using the server-backed PDF Viewer.
Step 1: Create a Simple PDF Viewer Sample in Vue
Start by following the steps in this guide to create a simple PDF viewer sample in Vue. This establishes a basic PDF Viewer component and its dependencies.
Step 2: Modify the PdfViewerController.cs file in the web service project
-
Create a web service project in .NET Core 3.0 or above. See this background article for the PDF Viewer web service pattern
-
Open the
PdfViewerController.csfile in the web service project. -
Import the required namespaces at the top of the file:
using System.IO;
using Google.Cloud.Storage.V1;
using Google.Apis.Auth.OAuth2;- Add the following private fields and constructor parameters to PdfViewerController. In the constructor, assign values from configuration to the corresponding fields.
// Private readonly object _storageClient
private readonly StorageClient _storageClient;
private IConfiguration _configuration;
public readonly string _bucketName;
public PdfViewerController(IWebHostEnvironment hostingEnvironment, IMemoryCache cache, IConfiguration configuration)
{
_hostingEnvironment = hostingEnvironment;
_cache = cache;
// The key file is used to authenticate with Google Cloud Storage.
string keyFilePath = "path/to/service-account-key.json";
// Load the service account credentials from the key file.
var credentials = GoogleCredential.FromFile(keyFilePath);
// Create a storage client with Application Default Credentials
_storageClient = StorageClient.Create(credentials);
_configuration = configuration;
_bucketName = _configuration.GetValue<string>("BucketName");
}- Modify the
Load()method to load the PDF files from Google Cloud Storage.
[HttpPost("Load")]
[Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]
[Route("[controller]/Load")]
//Post action for Loading the PDF documents
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
MemoryStream stream = new MemoryStream();
object jsonResult = new object();
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
if (bool.Parse(jsonObject["isFileName"]))
{
string bucketName = _bucketName;
string objectName = jsonObject["document"];
_storageClient.DownloadObject(bucketName, objectName, stream);
stream.Position = 0;
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}- Open
appsettings.jsonin the web service project and add the following keys below the existingAllowedHostsconfiguration
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"BucketName": "Your Bucket name from Google Cloud Storage"
}NOTE
Replace the placeholder with the actual Google Cloud Storage bucket name.
NOTE
Replace
path/to/service-account-key.jsonwith the configured file path or provide credentials through a secure mechanism (environment variable or secrets manager). Do not store service account keys in source control.
Step 3: Configure the PDF Viewer component
Set the serviceUrl to the web service endpoint (replace the localhost URL with the server URL). Set documentPath to the PDF file name to load from Google Cloud Storage. Ensure the document name exists in the bucket.
<template>
<div id="app">
<ejs-pdfviewer id="pdfViewer" :serviceUrl="serviceUrl" :documentPath="documentPath">
</ejs-pdfviewer>
</div>
</template>
<script setup>
import { provide } from "vue";
import {
PdfViewerComponent, 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 the 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 the 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
The
Google.Cloud.Storage.V1NuGet package must be installed in the web service project to use the previous code example.