How to convert HTML text to PDF in salesforce using apex with Aspose Cloud APIs

@tyagias001

Please note you need to escape the HTML string. As HTML text is escaped in above shared example and mentioned in the Insert HTML documentation. So the HTML input text would be like as below.

<p style="text-align: center;"><b style="color: rgb(102, 102, 102); font-size: 9pt;">秘密保持契約書 および売主またはそのいずれかは、本契約の日付の前後いずれに提供されたかを問わないすべての情報(以下</b></p>

@tilal.ahmad

So are you saying that we need to makes changes to input string(which is a user entry rich text field).?
In that case again lot of backend processing needs to happen and have to write code for escaping HTML characters?

Is they a better way of doing this.?

@tyagias001

I am afraid there is no other option available to add HTML text in a Word document and then convert it to PDF. Other options are file conversion. I think you can simply replace following characters in HTML string using replace method.

" is replaced with "
& is replaced with &
< is replaced with &lt;
> is replaced with &gt;

@tilal.ahmad

SO the first call is successfully. Mail merge was successful.

Now the pdf download url - https://api.aspose.cloud/v4.0/words/MailMergeOut(1).doc?format=pdf has below content -

{
“Error”: {
“Code”: “error”,
“Message”: “ClientId is undefined. Please check authorization.”,
“Description”: “Operation Failed. General Error.”
}
}

String strURl = 'https://api.aspose.cloud/v4.0' + '/words/' + 'template.doc' + '/MailMerge?destFileName=MailMergeOut(1).doc';

    		//String inputText = '<p style="text-align: center;"><b style="color: rgb(102, 102, 102); font-size: 9pt;">秘密保持契約書 および売主またはそのいずれかは、本契約の日付の前後いずれに提供されたかを問わないすべての情報(以下</b></p>'; 
    		String inputText = '&lt;p style=&quot;text-align: center;&quot;&gt;&lt;b style=&quot;color: rgb(102, 102, 102); font-size: 9pt;&quot;&gt;秘密保持契約書 および売主またはそのいずれかは、本契約の日付の前後いずれに提供されたかを問わないすべての情報(以下&lt;/b&gt;&lt;/p&gt';
			            
            String jsonData= '{"root":{"data":{"format": "html","htmlText": "' + inputText + '"}}}';
            system.debug('jsonData ' + jsonData );
            String strJSON1 = ProcessCommand(strURl, 'PUT', jsonData, 'json', GetJWT());
            
            system.debug('strJSON1 @@ ' + strJSON1); 
            
            String downloadURL = null;
            
            Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(strJSON1);
            
            if(params.containsKey('Document')){
                Map<String, Object> doc = (Map<String, Object>)params.get('Document');
                String DocName = (String)doc.get('FileName');
                
                strURl = 'https://api.aspose.cloud/v4.0' + '/words/' + DocName + '?format=' + outFormat;
                
                downloadURL = strURl;
            }
            system.debug('downloadURL '+ downloadURL);
            return downloadURL;
            
        }

@tyagias001

I am afraid you can not get the PDF document directly. You need to make a request using the download URL. Please check the sample code as following and use the response as per your requirement.

        String strURI='https://api.aspose.cloud/v4.0/words/MailMergeOut(1).doc?format=pdf';
        System.debug(strURI);
        request.setEndpoint(strURI);
        request.setMethod('GET');                
        request.setHeader('Authorization', 'Bearer ey........FDbcg');              
        request.setHeader('Accept', 'application/pdf');     
           
        Http http = new Http();
        HttpResponse res = http.send(request);
        return res.getBodyAsBlob();

@tilal.ahmad

Thanks for the resolving all my queries … Process is very lengthy.
Now if i dont want to store the data on aspose cloud , then what i need to do?
After i get the final BLOB fall the doc files should get deleted.

@tyagias001

Thanks for your feedback. We already logged an enhancement ticket(WORDSCLOUD-364) to Mail Merge and save the output in PDF in a single API call. We have linked your post to the ticket and will notify you as soon as it gets available.

There are two options either to delete files on request or set your cloud storage mode for the file retention period.

Can file retention set for few hours

@tyagias001

I am afraid the minimum file retention time is 24 hours. Please note Aspose Cloud storage is encrypted and it is safe to use it. However, if you are not comfortable with it then you can use other supported 3rd party cloud storages of your choice.

Ok… So i am thinking there is no language restriction for conversion of html into pdf.
So it essentially means html containing text of any language(french, japan, chinese etc) would be converted to pdf?

Another question i have is what will happen if a html contains Image and plain text both in a single input?

Will this get converted to pdf? if yes how pdf will look like?

@tyagias001

Yes, your understanding is correct.

Yes, it should work. Please check sample JSON text in above shared example. It includes images. However, if you face any issue with some HTML Text then please share here. We will look into it and will guide you.

The issues you have found earlier (filed as WORDSCLOUD-364) have been fixed in this update. This message was posted using Bugs notification tool by Ivanov_John