I use Javascript and Google Drive API to get PDF, but I can't parse the file correctly: the number of pages is correct, but the content is blank.
I'm pretty sure the function which help the browser read typedarray in the pdf.js can work, because it's also used to convert PDF picked up from the HTML input tag.
I have found this Q&A, but this method is no longer useful. It seems that Google has changed the method of API.
I have also found this, and then I tried to use new File()
rather than new Blob()
. Unfortunately, it's still failed.
By the way, the webViewLink and the webContentLinl both work. I can use them to download or preview PDF.
Is there any other way to solve the problem? Thank you for the help.
Here's my code
createPicker() {
const view = new window.google.picker.View(window.google.picker.ViewId.DOCS);
view.setMimeTypes('application/pdf');
const picker = new window.google.picker.PickerBuilder()
.enableFeature(window.google.picker.Feature.NAV_HIDDEN)
.enableFeature(window.google.picker.Feature.MULTISELECT_ENABLED)
.setDeveloperKey(process.env.GOOGLE_CONFIG.PICKER_API_KEY)
.setAppId(process.env.GOOGLE_CONFIG.APP_ID)
.setOAuthToken(this.accessToken)
.addView(view)
.addView(new window.google.picker.DocsUploadView())
.setCallback(this.pickerCallback)
.build();
picker.setVisible(true);
},
async pickerCallback(data) {
if (data.action === window.google.picker.Action.PICKED) {
const document = data[window.google.picker.Response.DOCUMENTS][0];
const fileId = document[window.google.picker.Document.ID];
try {
const response = await window.gapi.client.drive.files.get({
fileId,
alt: 'media',
});
const blob = new Blob([response.body], { type: document.mimeType });
const arrayBuffer = await blob.arrayBuffer();
const typedarray = new Uint8Array(arrayBuffer);
// TODO: use pdf.js read file
} catch (error) {
console.error('Error getting file content:', error);
}
}
},