Saving Annotated PDF Files Using PDF.js

I’ve been working on a project where I need to save a PDF file after annotations have been added using PDF.js. Despite being able to successfully download the file with annotations, I’ve been unable to directly save the annotated file through my implementation.

Is it possible to save a PDF file with annotations using PDF.js? If so, could someone kindly provide a small demo or guide on the method you used to achieve this? Any tips, best practices, or code snippets would be greatly appreciated.

Thank you in advance for your assistance. I believe that if downloading the file with annotations is feasible, then saving it should be possible as well, and I’m eager to learn the right approach.

This is what I have so far, im able to get all the form fields except the annotated file on the backend,

<script>

document.addEventListener('DOMContentLoaded', ()= > {
    const iframe= document.getElementById('iframe_pdfjs')

    iframe.onload= function() {
        const iframeWindow = iframe.contentWindow

        // Listen for pagesloaded event to ensure PDF.js is fully initialized
        iframeWindow.addEventListener('pagesloaded', function() {
            console.log('PDF.js pages loaded')

            // Debugging: Log the PDFViewerApplication and related objects
            console.log('PDFViewerApplication:',
                        iframeWindow.PDFViewerApplication)
            console.log('PDF Viewer:',
                        iframeWindow.PDFViewerApplication.pdfViewer)
            console.log('Annotation Storage:',
                        iframeWindow.PDFViewerApplication.pdfViewer.annotationStorage)
            console.log('Annotation Editor Layer:',
                        iframeWindow.PDFViewerApplication.pdfViewer.annotationEditorLayer)

            // Try to access annotationStorage directly
            const annotationStorage = iframeWindow.PDFViewerApplication.pdfViewer.annotationStorage
            if (!annotationStorage) {
                console.error('Annotation storage not available')
                alert('Annotations could not be retrieved.')
                return
            }

            const annotations = annotationStorage.serialize()
            if (!annotations) {
                console.error('Annotations are null or undefined')
                alert('Failed to serialize annotations.')
                return
            }

            // Proceed to save the document
            document.getElementById('saveForm').addEventListener('submit', async function(event) {
                event.preventDefault()
                try {
                    const pdfDoc = iframeWindow.PDFViewerApplication.pdfDocument
                    const data = await pdfDoc.saveDocument({xfdfString: annotations})
                    const blob = new Blob([new Uint8Array(data)], {type: 'application/pdf'})

                    // Log to confirm the blob was created
                    console.log('Blob created:', blob)
                    console.log('Blob size:', blob.size)

                    const formData = new FormData()
                    formData.append('signed_document', blob,
                                    'signed_document.pdf')
                    formData.append('first_name', document.getElementById(
                        'id_first_name').value)
                    formData.append('last_name', document.getElementById(
                        'id_last_name').value)
                    formData.append(
                        'email', document.getElementById('id_email').value)
                    formData.append(
                        'phone', document.getElementById('id_phone').value)
                    formData.append(
                        'dob', document.getElementById('id_dob').value)
                    formData.append(
                        'gender', document.getElementById('id_gender').value)

                    const response= await axios.post('.', formData, {
                        headers: {
                            'X-CSRFToken': '{{ csrf_token }}',
                            'Content-Type': 'multipart/form-data'
                        }
                    })

                    if (response.status == = 200) {
                        window.location.href = '{% url "sign:thank_you" %}'
                    } else {
                        alert('Failed to save the signed document.')
                    }
                } catch(error) {
                    console.error('There was an error!', error)
                    alert('Failed to save the signed document.')
                }
            })
        })
    }
});
</script>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật