Unable to Preview Downloaded PDF files in Google Drive

Hi,

I’m downloading a file from Google Drive Node, but the preview is empty (as shown below)

It seems like file PDF files are getting corrupted while downloading, that’s why preview is not available. I tried uploading the same PDF file in google drive from the previous node (where I downloaded the pdf), but the uploaded file is corrupted. I’m not getting error while downloading and uploading the PDF files.

Here is the code in n8n for downloading the files in google drive node. I’m facing the same issue in download in Box node as well, and we’re using the similar code there as well.

else if (operation === 'download') {
					// ----------------------------------
					//         download
					// ----------------------------------

					const fileId = this.getNodeParameter('fileId', i) as string;

					const response = await drive.files.get(
						{
							fileId,
							alt: 'media',
						},
						{
							responseType: 'arraybuffer',
						},
					);

					let mimeType: string | undefined;
					if (response.headers['content-type']) {
						mimeType = response.headers['content-type'];
					}

					const newItem: INodeExecutionData = {
						json: items[i].json,
						binary: {},
					};

					if (items[i].binary !== undefined) {
						// Create a shallow copy of the binary data so that the old
						// data references which do not get changed still stay behind
						// but the incoming data does not get changed.
						Object.assign(newItem.binary, items[i].binary);
					}

					items[i] = newItem;

					const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i) as string;

					const data = Buffer.from(response.data as string);

					items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(data as unknown as Buffer, undefined, mimeType);

kindly let me know how to fix this issue, seems like reading the response data as string might be causing the issue. Thanks.

Hey @Shahtaj_Khalid,

If you write the data to disk then view it is it also empty? It would be handy to know if it is just a UI issue or if the actual download is failing.

I was able to replicate the issue. It only happens when the PDF is very large. The PDF that I used to replicate the issue has 250 pages. However, the data is not corrupted. The bug seems to be only on the client-side not loading the PDF.

1 Like

I tried writing the response in pdf file, the final file is empty as well.

this is the code I tried to write the response in file.

fs.writeFile("testPDF.pdf", apiResponse, 'binary', (err) => {
						if (err)
							console.log(err);
						else {
							console.log("File written successfully\n");
							console.log("The written has the following contents:");
							console.log(fs.readFileSync("testPDF.pdf", "utf8"));
						}
						});

I’m facing the issue with a PDF with single page as well, with a size 17.1 KB.

UPDATE: Turns out it was happening due to the underlying request library we’re using for API calls. Setting the encoding option to null fixed the issue.

3 Likes