Merge Documents From URL - Salesforce
PDF Merge sample in Salesforce demonstrating ‘Merge Documents From URL’
MergeDocumentsToPDF.cls
public class MergeDocumentsToPDF {
static String API_KEY = '********************';
string destinationFile = 'MergeResultByURL';
public void MergeDocToPDF()
{
try
{
Map<string, Object> parameters = new Map<string, Object>();
// You can also upload your own file into PDF.co and use it as url. Check "Upload File" samples for code snippets: https://github.com/bytescout/pdf-co-api-samples/tree/master/File%20Upload/
parameters.put('url', 'https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-merge/sample1.pdf,https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/other/Input.xls, https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-merge/images-and-documents.zip');
string jsonPayload = Json.serialize(parameters);
string url = 'https://api.pdf.co/v1/pdf/merge2';
HttpRequest req1 = new HttpRequest();
req1.setBody(jsonPayload);
req1.setHeader('x-api-key', API_KEY);
req1.setHeader('Content-Type', 'application/json');
req1.setEndpoint(url);
req1.setMethod('POST');
req1.setTimeout(60000);
Http http = new Http();
HTTPResponse res1 = http.send(req1);
if(res1.getStatusCode() == 200)
{
Map<String, Object> deserializedBody = (Map<String, Object>)JSON.deserializeUntyped(res1.getBody());
Boolean isError = Boolean.ValueOf(deserializedBody.get('error'));
if(isError == false)
{
SYstem.debug('res1.getBody() :: '+res1.getBody());
String urlVal = (String)deserializedBody.get('url');
downloadPDFAndStore(urlVal, destinationFile);
}
}
else
{
System.debug('Success Response ' + res1.getBody());
System.Debug(' Status ' + res1.getStatus());
System.Debug(' Status Code' + res1.getStatusCode());
System.Debug(' Status String' + res1.toString());
}
}
catch(Exception ex)
{
String errorBody = 'Message: ' + ex.getMessage() + ' -- Cause: ' + ex.getCause() + ' -- Stacktrace: ' + ex.getStackTraceString();
System.Debug(errorBody);
}
}
@TestVisible
private static void downloadPDFAndStore(String extFileUrl, String destinationFile)
{
try
{
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);
//Now Send HTTP Request
HttpResponse res = h.send(req);
if(res.getStatusCode() == 200)
{
blob fileContent = res.getBodyAsBlob();
ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S'; // to use S specify this document is in Salesforce, to use E for external files
conVer.PathOnClient = 'result' + '.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('Success Response ' + res.getBody());
System.Debug(' Status ' + res.getStatus());
System.Debug(' Status Code' + res.getStatusCode());
System.Debug(' Status String' + res.toString());
}
}
catch(Exception ex)
{
String errorBody = 'Message: ' + ex.getMessage() + ' -- Cause: ' + ex.getCause() + ' -- Stacktrace: ' + ex.getStackTraceString();
System.Debug(errorBody);
}
}
}
MergeDocumentsToPDFTest.cls
@isTest
private class MergeDocumentsToPDFTest {
private static testmethod void MergeDocToPDFTest(){
Test.setMock(HttpCalloutMock.class, new MergeDocToPDF());
Test.startTest();
MergeDocumentsToPDF mdp = new MergeDocumentsToPDF();
mdp.MergeDocToPDF();
Test.stopTest();
}
private static testmethod void MergeDocToPDFTestforCatch(){
Test.startTest();
MergeDocumentsToPDF mdp = new MergeDocumentsToPDF();
mdp.MergeDocToPDF();
Test.stopTest();
}
private static testmethod void downloadPDFAndStoreTest(){
Test.startTest();
Test.setMock(HttpCalloutMock.class, new MergeDocumentsToPDFMcok.MergeDocToPDF());
MergeDocumentsToPDF.downloadPDFAndStore('https://pdf-temp-files.s3.amazonaws.com/3ec287356c0b4e02b5231354f94086f2/result.pdf', 'result.pdf');
Test.stopTest();
}
private static testmethod void downloadPDFAndStoreTestforCatch(){
Test.startTest();
MergeDocumentsToPDF.downloadPDFAndStore(null, null);
Test.stopTest();
}
public class MergeDocToPDF implements HttpCalloutMock{
public HTTPResponse respond(HTTPRequest req) {
String testBody = '{"url":"https://pdf-temp-files.s3.amazonaws.com/839a4e2f10e249739ed0c167b453e36b/multipagedInvoiceWithQRCode.pdf","pageCount":1,"error":false,"status":200,"name":"newDocument","remainingCredits":9913694,"credits":3}';
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json;charset=UTF-8');
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.
Download Source Code (.zip)
return to the previous page explore PDF Merge endpoint
Copyright © 2016 - 2023 PDF.co