function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bharath Kumar ThotaBharath Kumar Thota 

Saving PDF from VisualForce and when tried to opening it is displaying "Failed to load PDF document"

Hi,
I am adding an Attachment for a record in Account. It is saving the attachment as PDF for the record but when I try to open it showing "Failed to load PDF document". I am using RemoteAction for saving the attachment. Please can I know why?

This is my method in controller:
@RemoteAction
    public static void saveHTMLPDF(Id ehsId, String signatureBody) {
        Attachment a = new Attachment(ParentId = ehsId, name = 'pdfFromVf.pdf', ContentType = 'application/pdf', 
                                      Body = Blob.valueOf(signatureBody));
        System.debug('1. ehsId: ' + ehsId + ' signatureBody: ' + signatureBody);
        System.debug('2. Attachement a: ' + a);
        Database.saveResult result = Database.insert(a, false);
    }

VisualForce page code:
<script type = "text/javascript">
    function savePdf() {
        var x = document.getElementById("savePDF");
        x.style.display = "none";
        var ehsId = "0012x000006LNsN";
        var pdf = new jsPDF();
        pdf.addHTML(document.body, function() {
            Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.HTMLSignatureCodeController.saveHTMLPDF}', 
            ehsId, pdf,
            function(result, event){
            },
            {escape: true}
        );
        });
   }
    </script>

Thank you,
Bharath Kumar Thota
VinayVinay (Salesforce Developers) 
Hi Bharath,

Do you see error in every browser or specific to chrome?

Review below links which can help you.

https://support.google.com/chrome/forum/AAAAP1KN0B0zLpFop8F58A/?hl=en&gpf=%23!topic%2Fchrome%2FzLpFop8F58A
https://salesforce.stackexchange.com/questions/169105/apex-uploading-pdf-failed-to-load
https://salesforce.stackexchange.com/questions/169105/apex-uploading-pdf-failed-to-load

Thanks,
Vinay Kumar
Bharath Kumar ThotaBharath Kumar Thota
Hi Vinay,
Thank you so much for your reply.
I am getting the error in Chrome, Edge and Internet Explorer also.

Thanks,
Bharath Kuamr Thota