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
Michael PaulsonMichael Paulson 

pageReference.getContentAsPDF losing view state

I'll try to keep this simple as I suspect the answer might be something where there is a known workaround, but I just can't find that.
I am accepting input from the user and kicking it out in a PDF. This is being done through an iFrame (I'm also using the same code to save a PDF). So the relevant parts are below:
Apex:
PageReference ref = Page.myPDF;
ref.getParameters().put('id', account);
Blob contentData = ref.getContentAsPDF(); //THIS LINE IS WHERE THE PROBLEM COMES IN
pageRefPDF = 'data:application/pdf;base64,' + EncodingUtil.base64Encode(contentData);

Visualforce:
<apex:iframe id="frame" src="{!pageRefPDF}"/>

I'm letting the user add and remove rows of data and I'm confident I am doing this correctly because I have traced the error to a particular variable and am running debug lines on that variable right before the problem line (above). When adding a new row and saving, the debug shows the variable equaling what it should (0,1,2,3,4,5), but when I save and kick out the PDF, I am getting a 'Subscript value 6 not valid. Must be between 0 and 5' error. It is also working if I comment out the getContentAsPDF line and display the page directly rather than through an iFrame (but I need to do this for a couple reasons). Is there a known issue and workaround having to do with the getContentAsPDF method that I should be aware of?

Sorry to be vague, and let me know if more of my code would help, but I'm assuming, given the above reasoning, that the issue is related to some Apex quirk rather than something I'm doing incorrectly. Thanks in advance!
SandhyaSandhya (Salesforce Developers) 
Michael PaulsonMichael Paulson
Thanks for your research, but the subscript error only happens when using getContentAsPDF (or just getContent as well). The exact same code works as expected if I remove this line. I have tried separating the 'save' function into two separate functions to handle the timing of the getContent methods, but I'll have to try a few more variations of that and cross my fingers.