Link Search Menu Expand Document

PDF From Html Template - Salesforce

PDF from HTML Template sample in Salesforce demonstrating ‘PDF From Html Template’

PdfFromHtmlTemplate.cls
public class PdfFromHtmlTemplate {

    String API_KEY = '************************';
    string DestinationFile = 'PdfFromHtmlTemplate'; 
    string endPointUrl = 'https://api.pdf.co/v1/pdf/convert/from/html';
    
    public void pdfFromHtml()
    {
        try
        {  
            String jsonBody = '{ "templateId": 2, "name": "newDocument.pdf", "mediaType": "print", "margins": "40px 20px 20px 20px", "paperSize": "Letter", "orientation": "Portrait", "printBackground": true, "header": "", "footer": "", "async": false, "encrypt": false, "templateData": "{\'paid\': true,\'invoice_id\': \'0021\',\'invoice_date\': \'August 29, 2041\',\'invoice_dateDue\': \'September 29, 2041\',\'issuer_name\': \'Sarah Connor\',\'issuer_company\': \'T-800 Research Lab\',\'issuer_address\': \'435 South La Fayette Park Place, Los Angeles, CA 90057\',\'issuer_website\': \'www.example.com\',\'issuer_email\': \'info@example.com\',\'client_name\': \'Cyberdyne Systems\',\'client_company\': \'Cyberdyne Systems\',\'client_address\': \'18144 El Camino Real, Sunnyvale, California\',\'client_email\': \'sales@example.com\',\'items\': [ {\'name\': \'T-800 Prototype Research\',\'price\': 1000.0 }, {\'name\': \'T-800 Cloud Sync Setup\',\'price\': 300.0 }],\'discount\': 0.1,\'tax\': 0.0725,\'note\': \'Thank you for your support of advanced robotics.\'}" } ';
            Http http = new Http();
            HttpRequest request = new HttpRequest();            
            request.setHeader('x-api-key', API_KEY);
            request.setEndpoint(endPointUrl);
            request.setHeader('Content-Type', 'application/json');
            request.setMethod('POST');            
            request.setBody(jsonBody);            
            HttpResponse response =  http.send(request);            
            Map<String, Object> json = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());            
            if(response.getStatusCode() == 200) 
            {
                if ((Boolean)json.get('error') == false)
                {
                    System.debug('response.getBody() :: '+response.getBody());
                    String resultFileUrl =(String)json.get('url');
                    downloadFile(resultFileUrl, DestinationFile);
                    System.debug('Generated PDF file saved as \'{0}\' file.'+ DestinationFile);
                }
            }
            else
            {
                System.debug('Error Response ' + response.getBody());
                System.Debug(' Status ' + response.getStatus());
                System.Debug(' Status Code' + response.getStatusCode());
                System.Debug(' Response String' + response.toString());
            }
        }
        catch (Exception ex)
        {
            String errorBody = 'Message: ' + ex.getMessage() + ' -- Cause: ' + ex.getCause() + ' -- Stacktrace: ' + ex.getStackTraceString();
            System.Debug(errorBody);
        }
    }
    
    @TestVisible
    private static void downloadFile(String extFileUrl, String DestinationFile)
    {
        Http h = new Http(); 
        HttpRequest req = new HttpRequest(); 
        extFileUrl = extFileUrl.replace(' ', '%20'); 
        req.setEndpoint(extFileUrl); 
        req.setMethod('GET'); 
        req.setHeader('Content-Type', 'application/pdf');
        req.setCompressed(true); 
        req.setTimeout(60000); 
        HttpResponse res  = h.send(req); 
        if(res.getStatusCode() == 200) 
        {
            blob fileContent = res.getBodyAsBlob();
            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S'; 
            conVer.PathOnClient = DestinationFile + '.pdf'; // The files name, extension is very important here which will help the file in preview.
            conVer.Title = DestinationFile; // Display name of the files
            conVer.VersionData = fileContent;
            insert conVer;
            System.Debug('Success');
        }
        else
        {
            System.debug('Error Response ' + res.getBody());
            System.Debug(' Status ' + res.getStatus());
            System.Debug(' Status Code' + res.getStatusCode());
            System.Debug(' Response String' + res.toString());
        }
    }
}
PdfFromHtmlTemplateTest.cls
@isTest
private class PdfFromHtmlTemplateTest {

    static testmethod void testPdfFromHtmlTemplate()
    {
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new PdfFromHtmlTemplateTest.PdfFromHtmlTemplateMock());
        PdfFromHtmlTemplate pdfToHtml = new PdfFromHtmlTemplate();
        pdfToHtml.pdfFromHtml();
        Test.stopTest();
    }
    
    static testmethod void testPdfFromHtmlTemplateForCatch()
    {
        Test.startTest();
        PdfFromHtmlTemplate pdfToHtml = new PdfFromHtmlTemplate();
        pdfToHtml.pdfFromHtml();
        Test.stopTest();
    }
    
    public class PdfFromHtmlTemplateMock implements HttpCalloutMock {
        public HttpResponse respond(HTTPRequest req) {
            HttpResponse res = new HttpResponse();
            String testBody = '{ "url": "https://pdf-temp-files.s3.us-west-2.amazonaws.com/I2K4REAIUDL5VJXG3WR10SUI7WPTSLD2/newDocument.pdf?X-Amz-Expires=3600&X-Amz-Security-Token=FwoGZXIvYXdzEHAaDBbojXVomGyhj38pUSKCATL90aSv8rG9O6XHS66qBmgm%2FCnX8DpXzL1M2Zcs43pyK1dn%2Fa6TLlyUal2%2Bzda%2BipdNBljLhyjY5SXzNARe56f0AEUjEHYHfjCe6O3ejZs33WbNYD1qBfMiONcWaEA25Dt%2FV0ukP8ixVMVlbNJWGAaF6iz2Laax5NxdB3%2B3IBGBBI4oge2XoAYyKC0Q1BwVxGl78nymJAkUDDrO0WISEusfCKB4ldP%2Bv%2F2lTbef0NZSHDE%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA4NRRSZPHGEJLXAAK/20230306/us-west-2/s3/aws4_request&X-Amz-Date=20230306T155847Z&X-Amz-SignedHeaders=host&X-Amz-Signature=62cfc8ca245dee65c3aadb50e479d81c87443c031df743b841fb5e11cac87c96", "pageCount": 1, "error": false, "status": 200, "name": "newDocument.pdf", "credits": 9, "duration": 3001, "remainingCredits": 1165546 } ';
            res.setHeader('Content-Type', 'application/json');
            res.setBody(testBody);
            res.setStatusCode(200);
            return res;
        } 
    }
}

PDF.co Web API: the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.

Get your PDF.co API key here!

Download Source Code (.zip)

return to the previous page explore PDF from HTML Template endpoint