Hello all! I have an iframe with a pdf url inside it that displays pdf files stored in the PythonAnywhere filesystem. The goal is to be able to have the user edit the PDF, then save the modified PDF to its online location with the user's modifications.
I have tried using something like this:
function saveModifiedPdf(code) {
const pdfIframe = document.getElementById('pdfIframe');
const modifiedPdfUrl = pdfIframe.src;
fetch(modifiedPdfUrl)
.then(response => response.blob())
.then(pdfBlob => {
const formData = new FormData();
formData.append('code', code);
formData.append('pdf', pdfBlob, 'code.pdf');
console.log('Modified PDF Blob size:', pdfBlob.size);
console.log('Modified PDF Blob type:', pdfBlob.type);
const savePdfUrl = `/save-pdf/${code}`;
fetch(savePdfUrl, {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'application/pdf'
}
})
.then(response => {
if (response.status === 200) {
console.log('Modified PDF saved successfully!');
} else {
console.error('Failed to save modified PDF.');
}
})
.catch(error => {
console.error('Error:', error);
});
})
.catch(error => {
console.error('Error fetching modified PDF:', error);
});
}
The python route looks like this:
@app.route('/save-pdf/<code>', methods=['POST'])
def save_pdf(code):
# Get the modified PDF data from the request
modified_pdf_data = request.data
with open(f'/route/to/file.pdf', 'wb') as file:
file.write(modified_pdf_data)
However it appears that this method doesn't retrieve the user modified data from the pdf. I believe the reason is because the code is pulling information from the url, which hasn't been updated with the user's changes.
Does anyone have any idea how to save a file with user edits to a file inside the PythonAnywhere system?
Thanks!