Open PDF from Google Drive in JavaScript
17 Nov 20257 minutes to read
Follow these steps to load a PDF from Google Drive using the server-backed PDF Viewer.
Step 1 Set up Google Drive API
You must set up a project in the Google Developers Console and enable the Google Drive API. Obtain the necessary credentials to access the API. For more information, view the official link.
Step 2: Create a Simple PDF Viewer Sample in JavaScript
Start by following the steps provided in this link to create a simple PDF viewer sample in JavaScript. This will give you a basic setup of the PDF viewer component.
Step 3: 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.csfile in your web service project. -
Import the required namespaces at the top of the file:
using System.IO;
using Google.Apis.Drive.v3;
using Google.Apis.Util.Store;- Add the following private fields and constructor parameters to
PdfViewerController. In the constructor, assign values from configuration to the corresponding fields.
private IConfiguration _configuration;
public readonly string folderId;
public readonly string applicationName;
public readonly string credentialPath;
private static readonly string[] Scopes = { DriveService.Scope.DriveFile, DriveService.Scope.DriveReadonly};
public PdfViewerController(IWebHostEnvironment hostingEnvironment, IMemoryCache cache, IConfiguration configuration)
{
_hostingEnvironment = hostingEnvironment;
_cache = cache;
_configuration = configuration;
folderId = _configuration.GetValue<string>("FolderId");
credentialPath = _configuration.GetValue<string>("CredentialPath");
applicationName = _configuration.GetValue<string>("ApplicationName");
}- Modify the Load() method to load the PDF files from Google Drive.
[HttpPost("Load")]
[Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]
[Route("[controller]/Load")]
//Post action for Loading the PDF documents
public async Task<IActionResult> Load([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF viewer object with memory cache object
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 objectName = jsonObject["document"];
UserCredential credential;
using (var stream1 = new FileStream(credentialPath, FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream1).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true));
}
// Create Google Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationName,
});
// List PDF files in Google Drive
listRequest.Q = "mimeType='application/pdf' and '" + folderId + "' in parents and trashed=false";
listRequest.Fields = "files(id, name)";
var files = await listRequest.ExecuteAsync();
string fileIdToDownload = string.Empty;
foreach (var file in files.Files)
{
string fileId = file.Id;
string fileName = file.Name;
if (fileName == objectName)
{
// Save the matching fileId
fileIdToDownload = fileId;
break;
}
}
string fileIds = fileIdToDownload;
var request = service.Files.Get(fileIds);
await request.DownloadAsync(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": "*",
"FolderId": "Your Google Drive Folder ID",
"CredentialPath": "Your Path to the OAuth 2.0 Client IDs json file",
"ApplicationName": "Your Application name"
}NOTE
Replace the placeholders with your actual values: Google Drive Folder ID, Application name, and the path to the OAuth 2.0 client IDs JSON file.
NOTE
The FolderId part is the unique identifier for the folder. For example, if your folder URL is:
https://drive.google.com/drive/folders/abc123xyz456, then the folder ID isabc123xyz456.
Step 4: Configure the PDF Viewer component
Set the serviceUrl to your web service endpoint (replace the localhost URL with your server URL). Set documentPath to the PDF file name to load from Google Drive. Ensure the document name exists in your Drive folder.
import { PdfViewer, Toolbar, Magnification, Navigation, LinkAnnotation,ThumbnailView,
BookmarkView, TextSelection, Annotation, FormFields, FormDesigner} from '@syncfusion/ej2-pdfviewer';
PdfViewer.Inject( Toolbar,Magnification,Navigation, LinkAnnotation,ThumbnailView,
BookmarkView, TextSelection, Annotation, FormFields, FormDesigner);
let viewer: PdfViewer = new PdfViewer();
// Replace the "localhost:44309" with the actual URL of your server
viewer.serviceUrl = 'https://localhost:44396/pdfviewer';
viewer.appendTo('#pdfViewer');
viewer.load('PDF_Succinctly.pdf', null);NOTE
The Google.Apis.Drive.v3 NuGet package must be installed in your application to use the previous code example.