I’m working on a tool that lets users pick PDF files from their Google Drive and turn them into JPGs. But I’m stuck on the downloading part.
Here’s what I’m doing:
- I get the file data from Google Drive using their API.
- I try to send this data to my server using AJAX.
- On the server, I try to save the data as a PDF file.
The problem is, the saved PDFs are empty. When I compare the original PDF with the downloaded one in a hex editor, there are clear differences.
Here’s a simplified version of my client-side code:
function downloadPDF(fileId) {
gapi.client.drive.files.get({ fileId: fileId }).then(function(response) {
var xhr = new XMLHttpRequest();
xhr.open('GET', response.result.downloadUrl);
xhr.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);
xhr.onload = function() {
if (xhr.status === 200) {
var rawData = btoa(xhr.response);
$.post('./savepdf.php', rawData, function(response) {
console.log('File saved:', response.name);
});
}
};
xhr.send();
});
}
And here’s my server-side PHP:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pdfData = file_get_contents('php://input');
$decodedData = base64_decode($pdfData);
file_put_contents('file.pdf', $decodedData);
echo json_encode(['name' => 'file.pdf']);
}
Any ideas on how to fix this and get the PDFs with their content intact?
hey flyingleaf, i’ve dealt with this before. the issue is probably with how ur handling the binary data. try using xhr.responseType = ‘blob’ instead of arraybuffer. then create a FormData object with the blob and send that to ur server. on the PHP side, use move_uploaded_file() to save it. that should fix ur empty PDF problem
I’ve encountered a similar issue when working with Google Drive PDFs. The problem likely stems from how you’re handling the binary data. Instead of using btoa() on the client side, which is meant for text, try this approach:
Set xhr.responseType = ‘arraybuffer’ before sending the request.
In the onload function, create a Blob: var blob = new Blob([xhr.response], {type: ‘application/pdf’});
Use FormData to send the Blob: var formData = new FormData(); formData.append(‘file’, blob, ‘file.pdf’); $.ajax({
url: ‘./savepdf.php’,
type: ‘POST’,
data: formData,
processData: false,
contentType: false,
success: function(response) {
console.log(‘File saved:’, response.name);
}
});
On the PHP side, use:
$pdfData = file_get_contents($_FILES[‘file’][‘tmp_name’]);
file_put_contents(‘file.pdf’, $pdfData);
This should preserve the binary content of your PDFs. Let me know if you need any clarification!
I’ve encountered similar challenges when working with Google Drive files. The issue likely stems from how you’re handling the binary data of the PDF. A key modification you might want to consider is setting the responseType of your XMLHttpRequest to ‘blob’ before sending it. This ensures you’re dealing with the raw binary data correctly.
On the server side, you’ll want to handle the incoming data as a file upload rather than raw POST data. This approach should preserve the integrity of your PDF files. Make sure to use move_uploaded_file() in your PHP script to save the uploaded file.
If you’re still facing issues after these changes, it might be worth checking the file permissions on your server and ensuring that your PHP script has write access to the directory where you’re attempting to save the PDFs.