Sure,
First I need to add that when you open the file with “preview” (apple) or abobe reader it is render as checkbox correctly.
To explain the process, I get the file from S3 with python on my back end and then i converted it to base64 and pass it ton my js as such :
DOWNLOAD :
WebViewer({
path: '/static/formation/PDFJSExpress/lib', // path to the PDF.js Express'lib' folder on your server
licenseKey: 'Insert commercial license key here after purchase',
disabledElements: [
// 'viewControlsButton',
// 'viewControlsOverlay',
// "fitButton",
// "leftPanelButton",
// "shapeToolGroupButton"
"toolbarGroup-Annotate",
"toolbarGroup-Shapes",
"toolbarGroup-Insert",
"viewControlsButton",
"toggleNotesButton",
"selectToolButton",
"panToolButton",
"crossStampToolButton",
"checkStampToolButton",
"rubberStampToolGroupButton",
"eraserToolButton"
],
}, document.getElementById('viewer'))
.then(instance => {
instance.UI.setLanguage('fr');
instance.UI.loadDocument(base64ToBlob('{{base64_string}}'), { filename: 'myfile.pdf' });
});
Python code:
@staticmethod
def pdf_to_base64_on_s3(bucket, pdf_file_name):
s3 = boto3.resource(
service_name='s3',
region_name='eu-west-3',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY']
)
# s3 = boto3.client('s3', aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'], aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'])
obj = s3.Object(bucket, pdf_file_name)
string = obj.get()['Body'].read()
b64_encoded_pdf = base64.b64encode(string).decode('utf-8')
return b64_encoded_pdf
SAVING:
To save I use the API to merge the pdf and after that I convert the blob into a js file container to assign it to a file input in a post form to retreive my document in my backend to save it on S3 and in my database :
function saveBlobToServer(blob) {
var file = new File([blob], "{{formation_name}}");
let container = new DataTransfer();
container.items.add(file);
console.log(container)
file_input = document.getElementById("id_pdf-pdf");
file_input.files = container.files;
console.log(file_input.files)
document.getElementById("form-view").classList.toggle('hidden')
document.getElementById("button-view").classList.toggle('hidden')
}
// This in in the WebViewer wrapper
const { documentViewer, annotationManager } = instance.Core;
// a callback function to some "download" button
document.getElementById("send").addEventListener("click", () => {
const onSave = async () => {
document.getElementById("spinner").classList.toggle("hidden")
document.getElementById("send").disabled = true;
const xfdf = await annotationManager.exportAnnotations({ links: false, widgets: true });
const fileData = await documentViewer.getDocument().getFileData({});
const blob = new Blob([fileData], { type: 'application/pdf' });
const data = new FormData();
data.append('xfdf', xfdf);
data.append('file', blob);
data.append('license', "");
// Process the file
const response = await fetch('https://api.pdfjs.express/xfdf/merge', {
method: 'post',
body: data
}).then(resp => resp.json());
const { url, key, id } = response;
// Download the file
const mergedFileBlob = await fetch(url, {
headers: {
Authorization: key
}
}).then(resp => resp.blob())
// Do something with blob...
// save(mergedFileBlob)
saveBlobToServer(mergedFileBlob);
}
onSave()
});