Contact Support
Save PDF file to Google Cloud Storage
9 Aug 20235 minutes to read
To save a PDF file to Google Cloud Storage, you can follow the steps below
Step 1: Create a PDF Viewer sample in Angular
Follow the instructions provided in this link to create a simple PDF Viewer sample in Angular. 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 above. You can refer to this link for instructions on how to create a web service project.
-
Open the
PdfViewerController.cs
file in your 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 the
PdfViewerController
class, In the constructor, assign the values from the 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
Download()
method to save the downloaded PDF files to Google Cloud Storage bucket
[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 bucketName = _bucketName;
string fileName = jsonObject["documentId"];
// Convert the base64 string back to bytes
string result = Path.GetFileNameWithoutExtension(fileName);
byte[] documentBytes = Convert.FromBase64String(documentBase.Split(",")[1]);
// Upload the document to Google Cloud Storage
using (var memoryStream = new MemoryStream(documentBytes))
{
_storageClient.UploadObject(bucketName, result + "_downloaded.pdf", null, memoryStream);
}
return Content(documentBase);
}
- Open the
appsettings.json
file in your web service project, Add the following lines below the existing"AllowedHosts"
configuration
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"BucketName": "Your Bucket name from Google Cloud Storage"
}
NOTE
Replace Your Bucket name from Google Cloud Storage with the actual name of your Google Cloud Storage bucket
NOTE
Replace path/to/service-account-key.json with the actual file path to your service account key JSON file. Make sure to provide the correct path and filename.
Step 3: Set the PDF Viewer Properties in Angular PDF viewer component
Modify the serviceUrl
property of the PDF viewer component with the accurate URL of your web service project, replacing https://localhost:44396/pdfviewer
with the actual URL of your server. Set the documentPath
property of the PDF viewer component to the desired name of the PDF file you wish to load from Google Cloud Storage. Ensure that you correctly pass the document name from the files available in your bucket to the documentPath property.
import { Component, OnInit } from '@angular/core';
import { LinkAnnotationService, BookmarkViewService, MagnificationService,
ThumbnailViewService, ToolbarService, NavigationService,
TextSearchService, AnnotationService, TextSelectionService,
PrintService, FormDesignerService, FormFieldsService} from '@syncfusion/ej2-angular-pdfviewer';
@Component({
selector: 'app-container',
// specifies the template string for the PDF Viewer component
template: `<div class="content-wrapper">
<ejs-pdfviewer id="pdfViewer"
[serviceUrl]='service'
[documentPath]='documentPath'
style="height:640px;display:block">
</ejs-pdfviewer>
</div>`,
providers: [ LinkAnnotationService, BookmarkViewService, MagnificationService,ThumbnailViewService,
ToolbarService, NavigationService, AnnotationService, TextSearchService,
TextSelectionService, PrintService, FormDesignerService, FormFieldsService]
})
export class AppComponent implements OnInit {
// Replace the "localhost:44396" with the actual URL of your server
public service = 'https://localhost:44396/pdfviewer';
public documentPath = 'PDF_Succinctly.pdf';
}
NOTE
The Google.Cloud.Storage.V1 NuGet package must be installed in your application to use the previous code example.