function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jayesh Babu A VJayesh Babu A V 

Open pdf in another tab

I did a REST call from my apex class which include website url. This rest will return the pdf form of that website. This is the REST call:
@AuraEnabled
    public static String convertLinkToPdf(String websiteUrl) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://baseurl.com/');
        req.setMethod('GET');
        req.setHeader('Authorization', 'apikey xxxxxxxxxxxxxxxxx');
        req.setBody('{"url": "'+ websiteUrl+'","filename" : "abc.pdf"}');
        HttpResponse res = new Http().send(req);
        System.debug('STATUS: ' + res.getStatusCode());
        System.debug('RESPONSE: ' + res.getBody());
        
        return res.getBody();
    }
This call will convert 'websiteUrl' to a pdf format and return as the body. I want this pdf to be opened in a new browser tab. Is there any way to do that?
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jayesh,

So, I see that there is generally use of below to redirect to another URL can you try checking below code:
 
PageReference pageRef = new PageReference('http://www.google.com');
        pageRef.setRedirect(true);
        return pageRef;

Let me know if this helps.

Regards,
Anutej​​​​​​​
AnudeepAnudeep (Salesforce Developers) 
Hi Jayesh, 

You have to handle the PDF content returned first and use window.open() in your component to open the content in a new tab 
 
String base64Response = resp.getBody();
    Attachment attach = new Attachment();
    attach.contentType = 'application/octet-stream';
    attach.name = 'myfile111.pdf';
    attach.parentId = '001XXXXXXX';
    attach.body = EncodingUtil.base64Decode(base64Response); 
    insert attach;

 
Jayesh Babu A VJayesh Babu A V
When I run this code, I am gettin this error:
System.StringException: Unrecognized base64 character: %