I've a need to generate a PDF with dynamic content intercepted by end user in an editable input form.
So, I tried reading everything on the form into a string and passed the string as a URL parameter that is ready by my PDF generation visualforce page and is getting an error like this now:
ERROR Message:
URL starting with 'https://java-ability-7401-dev-ed--c.visualforce.com/apex/generatepdf?customFormData=%7B%22saveRecor'... is longer than the maximum allowed length of 4,096
Here is a brief excerpt from my code:
PageReference thePage = Page.generatePDF;
thePage.getParameters().put('Id', recordId);
thePage.getParameters().put('customFormData', customFormData);
thePage.getContent();
I'm getting error at this line thePage.getContent()
.
Anyway I can generate this PDF file. Please note that this data needs to be accepted from end user via an input form (i.e. this data is not saved in salesforce objects, so I can't fire SOQL and read it from there).
------ EDIT from provided comments & suggestions ------
PageReference thePage = Page.generatePDF;
thePage.getParameters().put('Id', recordId);
thePage.getParameters().put('tempId', templateId);
thePage.getParameters().put('type', type);
//thePage.getParameters().put('customFormData', customFormData);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + thePage.getUrl());
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer '+UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setBody('customFormData='+customFormData);
HttpResponse httpRes = h.send(req);
system.debug('httpRes '+ httpRes.getBody());
system.debug('httpRes blob'+ httpRes.getBodyAsBlob());
And this is not throwing me exception like before but now my PDFs are generated as corrupt.
The Debug Output looks correct to me and no errors as such:
16:04:29:175 CALLOUT_REQUEST [99]|System.HttpRequest[Endpoint=https://java-ability-7401-dev-ed.cs41.my.salesforce.com/apex/generatepdf?Id=a0f55000002e7gaAAA&tempId=a0655000003P0HiAAK&type=pdf, Method=POST] 16:04:29:237 CALLOUT_RESPONSE [99]|System.HttpResponse[Status=OK, StatusCode=200]
--------- EDIT 2 ------------
After more debugging it turns out the Visualforce PDF output is executed based on the Endpoint URL
of the HttpRequest
.
Visualforce Output PDF correctly if 'm using following Endpoint:
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + thePage.getUrl());
And Visualforce Output PDF is corrupt when endpoint is:
req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + thePage.getUrl());
But this endpoint change is troubling because if I use getSalesforceBaseUrl()
then it has to be provided into Remote Site Settings, and if I use 'getOrgDomainUrl()` then I can use it without Remote Site Setting entry.
Now, this behaviour is coming out differently only for Visualforce URL. If I access basic REST API endpoint with my above code to read a ListView or records, then same Authentication header works fine with getOrgDomainUrl()
endpoint.