DataSourceException: ASPOSE Service Exception

Okay, then please change the base url from https://api.aspose.cloud/v4.0/words to https://api-mirror.aspose.cloud/v4.0/words we tested the new url multiple times from salesforce developer console and it works stable

Hello,
We have got above exception for download API with mirror api base url : https://api-mirror.aspose.cloud/v4.0/words

Have you added api-mirror into the remote site settings?

Based on the code you shared with me long time ago, I have created tests that works perfectly with api-mirror
here is a full code I use

public static String GetJWT() {
        String grant_type = 'client_credentials';

        String client_id= 'creds';
        String client_secret= 'creds';	
        HttpRequest request = new HttpRequest();
          
        request.setEndpoint('https://api-mirror.aspose.cloud/v4.0/words/connect/token');
        request.setMethod('POST');                
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded');              
        request.setHeader('Accept', 'application/json');
           
        String body = 'grant_type=' + grant_type + '&client_id=' + client_id + '&client_secret='+ client_secret ;
        request.setBody(body);
        request.setTimeout(20000);
        Http http = new Http();
        HttpResponse res = http.send(request);
        String jsonInput = res.getBody();
        Map<String, Object> a =(Map<String, Object>)JSON.deserializeUntyped(jsonInput);
        return a.get('access_token').ToString();
    }

public static void testDownloadFile() {
        String token = GetJWT();
        String fileName = 'new_file.docx';
        String signedURI ='https://api-mirror.aspose.cloud/v4.0/words/storage/file/' + fileName;
        HttpRequest request = new HttpRequest();
            
        request.setEndpoint(signedURI);
        request.setMethod('GET');
        request.setHeader('Accept', 'application/json');
        if(token != null)
            request.setHeader('Authorization', 'Bearer '+token);
        request.setTimeout(99999);
        Http http = new Http();
        HttpResponse res = http.send(request);
        Blob file = res.getBodyAsBlob();
    }

Thanks for the code reference. We have integrated the same and see in the debug logs download File API is not returning error, however, The generated merged document seems corrupted. If we try to open it in browser it gives error as ‘Failed to load PDF document’. Observed same error when viewed attachment from generated Master document.
Screenshot 2025-09-18 at 10.24.17 PM.png (120.0 KB)

So in executeTemplateMerge API request, URL is pointing out to mirrored base URL but in response it resturns old url http://api.aspose.cloud/v4.0/. (Please refer ‘Aspose Template Merge - Raw Response:’ in attached debug logs)
Debug logs:
Aspose 4.0 Execute Template Merge.docx (376.7 KB)

Can you Please confirm if below request URL we are using is correct for Execute Merge Template or merged document is being generated at old base URL? What changes we need to make to get correct merged template?

https://api-mirror.aspose.cloud/v4.0/words/fileName/MailMerge?withRegions=True&cleanup=ContainingFields,EmptyParagraphs,UnusedFields,UnusedRegions,RemoveTitleRow,RemoveTitleRowInInnerTables&useWholeParagraphAsRegion=False&destFileName=fileName

The mirror is used to avoid timeouts. The server that performs operations is still the same.
Please share the exact code you use to convert the merged document to a PDF. By default, after you execute mail merge, the file format is the same as it was in the template, so if your template is a docx file, after mail merge, it will still be a docx file.

We are using below code for Execute Template Merge API

 public String executeTemplateMerge(String FileName, String dataFile) {
        String DocName, httpRequestMethod;
        try {
            if(asposeVersion1_1){
                strURI = 'https://api.aspose.com/v1.1/words/'+ FileName + '/executeTemplate?cleanup=ContainingFields,EmptyParagraphs,UnusedFields,UnusedRegions,RemoveTitleRow,RemoveTitleRowInInnerTables';
                signedURI = AsposeUtils.Sign(strURI);
                httpRequestMethod = 'POST';
            }else{
// Please refer this code for Aspose 4.0 API execute Merge template Call
                FileName = FileName.replace(' ', '%20');         
                signedURI = 'https://api-mirror.aspose.cloud/v4.0/words/' + FileName + '/MailMerge?withRegions=True&cleanup=ContainingFields,EmptyParagraphs,UnusedFields,UnusedRegions,RemoveTitleRow,RemoveTitleRowInInnerTables&useWholeParagraphAsRegion=False&destFileName=merge_'+FileName;
                accessToken = GetJWT();
                httpRequestMethod = 'PUT';
            }
            String strJSON = ProcessCommand(signedURI, httpRequestMethod, dataFile, 'json', accessToken);
            return strJSON;
        
    }
 }

      public static String ProcessCommand(String strURI, String strHttpCommand, String strContent, String ContentType, String accessToken) {
        try {
            HttpRequest request = new HttpRequest();
            Integer len = 0;
            if (strContent != null && strContent != '') {
                request.setBody(strContent);
                len = strContent.length();
            }
            request.setEndpoint(strURI);
            request.setMethod(strHttpCommand);
            commonApiExecution(strURI,strHttpCommand,contentType,request,accessToken,len);
            if (contentType.toLowerCase() == 'xml'){
            request.setHeader('Content-Type', 'application/xml');
            }else if (contentType.toLowerCase() == 'json'){
            request.setHeader('Content-Type', 'application/json');
            }else{
            request.setHeader('Content-Type', 'MultiPart/Form-Data');
            }
            request.setHeader('Content-Length', String.valueOf(len));
            request.setHeader('Accept', 'application/json');
            if(accessToken != null)
            request.setHeader('Authorization', 'Bearer '+accessToken);
            request.setTimeout(99999);

            Http http = new Http();
            HttpResponse res = http.send(request);
            String responseBody = res.getBody();
            return responseBody;
        
        } catch (DataSourceException ex) {
            // Re-throw DataSourceException with original message
            throw ex;
        }
    }

In the code above, no conversion API is called. So, as your template is a docx file, the output is also a docx file even if you set filename to merged.pdf

We are mentioning output format in download API.
Please refer strURI in code below, ‘outFormat’ value is ‘pdf’
Note: Earlier shared code for execute merge template is same for Aspose 1.1 except different base URL and it is working fine for version 1.1. Issue is only for Aspose 4.0.

public Blob downloadFile(String fileName) {
        try {
        	Aspose.Credentials credentials = AsposeService.getCredentials();
         
                fileName = fileName.replace(' ', '%20');
               
                accessToken = GetJWT();
                
                if (String.isBlank(customFontFolder)){
                    strURI = 'https://api-mirror.aspose.cloud/v4.0/words/storage/file/' + fileName + '?format='+outFormat;
                }else{
                    strURI =  'https://api-mirror.aspose.cloud/v4.0/words/storage/file/' + fileName + '?fontsLocation=' + customFontFolder + '&format='+outFormat;
                }

            
           
            HttpRequest request = new HttpRequest();
            request.setEndpoint(strURI);
            request.setMethod('GET');
            request.setHeader('Accept', 'application/json');
            if(accessToken != null)
            request.setHeader('Authorization', 'Bearer '+accessToken);
            request.setTimeout(99999);
            Http http = new Http();
            HttpResponse res = http.send(request);
            Blob strJSON = res.getBodyAsBlob();
    
            return strJSON;
        }
        catch (Exception ex) {
            system.debug('ASPOSE File Download Service Exception : '+ex);
            throw new DataSourceException('ASPOSE File Download Service Exception : '+ex);
        }
    }

Because this API doesn’t perform conversion, it is just a file download from storage. If you want to get a file from storage but in a different format, you need to use a different API method.

Here is a reference for that method.

curl -v "https://api-mirror.aspose.cloud/v4.0/words?format="
-X GET
-H "Authorization: Bearer ############"

Thanks for the information.
We integrated the download API to get file in PDF format provided above. Now we are able to download the file with size less than 6MB.
When we upload docx template of size 3.4 MB, and generated PDF document is more than 6MB, we get the error as ‘ASPOSE File Download Service Exception : System.CalloutException: Exceeded max size limit of 6000000’
Is there any option where we can get file size of converted PDF document before download, so we can download the file in chunks if file size is larger?

I’m glad to hear that you can convert the file. I’m afraid our API doesn’t provide such an option. The 6 MB seems to be a Salesforce restriction, not ours.

@yaroslaw.ekimov
1. Custom Fonts
We are referring to Aspose customFonts folder in download URL. It works fine for Aspose 1.1.

https://api.aspose.com/v1.1/words/Live%20Speaker%20Program%20Invite%20(4).5a339b1e-ae6c-45f6-aa73-eb67b6edc9ce.docx?fontsLocation=customFonts&format=PDF&appSID=E8C4F52B-F08B-456F-AD91-6EC71C2A1896&signature=yCDALgvt7R3IWZ7%2FdnZe9KU6LXY

For Aspose 4.0 (default and mirrored version), when we add customFontsFolder in download URL, we get Read Timed Out error. We need the CustomFontFolder as without this clients using custom fonts will start experiencing issues. Please let us know if this is correct path to point to custom fonts folder.

https://api-mirror.aspose.cloud/v4.0/words/merge_Live%20Speaker%20Program%20Invite%20(4).docx?fontsLocation=customFonts&format=PDF
https://api.aspose.cloud/v4.0/words/merge_Live%20Speaker%20Program%20Invite%20(4).docx?fontsLocation=customFonts&format=PDF

Please let us know if you need more details for Apose 1.1 API.

2. Default Aspose URL vs Mirror API url:
Using earlier base url https://api.aspose.cloud/v4.0/ gives below exception in GetJWT API.
And works fine for Mirrored base url.

System.CalloutException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 503 Service Unavailable"

I will check the implementation to have the option of custom fonts.

Hi, do you have any update on this. Thank you!

I found that issue, and it’s planned to be solved. Once it is ready, I will contact you.

1 Like