0

I have an Aura component that download a JSON from an external REST service and I have to render the JSON in a PDF. How can I pass the JSON directly from the aura component in the visualforce page without saving the data some where?

Alternatively, is there a way to reassign the local variable of the visualforce when the page is rendering? What I would like to do is to have a variable in the visualforce that will have the same JSON-structure of the JSON of interest. At the moment I am saving the data in a proper field on a record and recalling it via the page controller, the problem is that I am not able to reassign the variable.

Just to be clear in my Aura Component I am opening the visualforce page as follows:

redirectToPdf: function(bigliettoId, component) {
        console.log('Redirecting to pdf...')
        try {
            window.open('/apex/PdfFattura?bigliettoId=' + bigliettoId)
        } catch (e) {
            console.log('Exception: ', JSON.parse(JSON.stringify(e)))
            console.log('notSuccess!');
            component.set("v.success", false)
            component.set("v.successMessage", false)
            component.set("v.error", true)
            component.set("v.messageError", "C'è stato un errore visualizzando la Fattura")
        }
    },

The visualforce is as follows:

<apex:page standardStylesheets="false" standardController="Biglietto__c" extensions="PdfFatturaCtrl" showHeader="false" sidebar="false" applyBodyTag="false" applyHtmlTag="False" >
    <head>
        <script type="text/javascript">
            function init() {
                console.log('onloading')
                let json = '{!json}'
                let fatturaParsed = '{!fattura}'
                // here i want to reassign the local variable fattura from the json that I retrived via the controller as:
                // fattura.FatturaElettronicaHeader
                // fattura.FatturaElettronicaBody
                // ...

            }
        </script>
    </head>
    <body onload="init()">
        <p>
            fattura header: {!fattura.FatturaElettronicaHeader}
        </p>
        <p>
            fattura Body: {!fattura.FatturaElettronicaBody}
        </p>
    </body>
</apex:page>

Actually I thought I could attach the data as body in the url and retrieve the data from the visualforce page, but I am in the same situation, how can I do it?

Thank you in advance for any hint.

2
  • how large is the json? too large to encode as a url parameter? alternatively, have you looked into rendering the visualforce page as a pdf from apex? you can do that via PageReference.getContentAsPDF(), giving you the Blob data representation of the pdf.
    – stackasaur
    Jul 16 at 12:31
  • Sorry if I did not answer you. Actually I found a feasible solution in which I created a class for the object and used the function System.JSON.deserialize(jsonStr,Fattura.class) casting as the object Fattura in the controller of the page.
    – Giovanni
    Jul 20 at 13:42
0

I solved with the following method:

  1. I created a class, with all the subclasses that I needed, to describe the json that I have to pass to the visualforce. The object, just to give the context, is named Fattura.
  2. In the controller, after retrieving the stringified json from the record, I used the function System.JSON.deserialize(jsonStr, Fattura.class) and casted to the desired object, i.e. Fattura.

This gave me the possibility to access the object as it will be a json in the visualforce page via the dot notation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.