PDF_Generation_and_Automation/force-app/main/default/classes/PDFGenerationProxy.cls
2025-08-30 17:07:35 +05:30

52 lines
2.1 KiB
OpenEdge ABL

public with sharing class PDFGenerationProxy {
@AuraEnabled
public static String generatePDFFromHTML(String htmlContent) {
try {
// Prepare the request
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://salesforce.tech4biz.io/generate-pdf');
request.setMethod('POST');
request.setHeader('Content-Type', 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
// Create multipart form data
String boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
String body = '';
body += '--' + boundary + '\r\n';
body += 'Content-Disposition: form-data; name="input"; filename="template.html"\r\n';
body += 'Content-Type: text/html\r\n\r\n';
body += htmlContent + '\r\n';
body += '--' + boundary + '--\r\n';
request.setBody(body);
request.setTimeout(120000); // 2 minutes timeout
// Make the callout
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
// Convert the PDF response to base64
Blob pdfBlob = response.getBodyAsBlob();
return EncodingUtil.base64Encode(pdfBlob);
} else {
throw new CalloutException('API call failed with status: ' + response.getStatusCode() + ' - ' + response.getBody());
}
} catch (Exception e) {
throw new AuraHandledException('PDF generation failed: ' + e.getMessage());
}
}
@AuraEnabled
public static String testAPIConnection() {
try {
// Test with simple HTML
String testHtml = '<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Test PDF Generation</h1></body></html>';
return generatePDFFromHTML(testHtml);
} catch (Exception e) {
throw new AuraHandledException('API test failed: ' + e.getMessage());
}
}
}