+ Start a Discussion

Create PDF displaying null page

I have taken an example to create a PDF from an LWC.  From an LWC component I am calling an Aura enabled class.  That class passes the HTML using a variable to a visualforce page that renders as a PDF.

In my own development org, I am getting the expected results.  However, when I use the exact same code in another sandbox, I am getting a null PDF.  I put a debug in the controller for the visualforce page.  The debug does not post in the code that is not working. Therefore, it seems that my pagereference is not working.

As I mentioned, the exact code is working in my development org, just not in the sandbox that I copy it to.  Any suggestions on why that woudl happen?  thanks!!!
SwethaSwetha (Salesforce Developers) 
HI fredka,
Check if have any hardcoded URLs/custom settings/custom labels in your code that might be causing the issue.

If this not the case, can you share the code I can use to reproduce in my org. 

If this information helps, please mark the answer as best. Thank you
Thanks for responding Swetha!  There is only one hardcode url in the javascript to open the attachment once it is created.  But that does not seem to be the issue.  This code is from a udemy course so I don't think there are any issues with the code.  I think something in my org is preventing this from working.  Prior to this I was trying to implement jspdf and also had issues.  That being said, here is the code I'm using.  

    <div class="container" style="background: white;
    padding: 16px;">
        <div style="margin-bottom: 50px;">
         <!--   <img src={imageUrl} style="height:40px; width:150px"/>  -->
            <div style="display: inline-block;float: right;">
                <div>Invoice #: {invoiceData.invoiceNo}</div>
                <div>Created #: {invoiceData.invoiceCreated}</div>
                <div>Due #: {invoiceData.invoiceDue}</div>
            <div style="display: inline-block;">
                <div> {invoiceData.companyName}</div>
                <div> {invoiceData.address1}</div>
                <div> {invoiceData.address2}</div>
            <div style="display: inline-block;float: right; text-align: right;">
                <div> {clientData.client}</div>
                <div> {clientData.username}</div>
                <div> {clientData.email}</div>
        <div style="background: #eee;
        margin: 16px 0px;
        padding: 8px;">
            <div  style="display: inline-block"><strong>Services</strong></div>
            <div style="display: inline-block;float: right;"><strong>Amount</strong></div>
        <template iterator:service={services}>
            <div key={service.value.name} style="padding: 8px;">
                <div style="display: inline-block">{service.value.name}</div>
                <div style="display: inline-block;float: right;">${service.value.amount}</div>
            <div if:true={service.last} key={service.value.name}  style="padding: 8px;">
                <div style="display: inline-block;float: right;"><strong>Total Amount:${totalAmount} </strong></div>

    <lightning-button variant="brand" label="generate pdf" onclick={pdfHandler}></lightning-button>

mport { LightningElement, api } from 'lwc';
import generatePDF from '@salesforce/apex/pdfController.generatePDF'
export default class PdfGenerationDemo extends LightningElement {
    @api recordId
   // imageUrl = 'https://www.sparksuite.com/images/logo.png'
        invoiceCreated:'January 1, 2019',
        invoiceDue:'January 10, 2020',
        companyName:'Sparksuite, Inc.',
        address1:'12345 Sunny Road',
        address2:' Sunnyville, CA 12345'
        client:'Acme Corp',
        username:'John Doe',
        {name:'Consultant fee', amount:1000.00},
        {name:'Website design', amount:300.00},
        {name:'Hosting (3 months)', amount:75.00}

    get totalAmount(){
        return this.services.reduce((total, service)=>{
            return total = total+service.amount
        }, 0)

        let content = this.template.querySelector('.container')
        generatePDF({ recordId:this.recordId, htmlData:content.outerHTML}).then(result=>{
            console.log("attachment id", result)


Apex class called from Javascript:
public with sharing class pdfController {

   public static Attachment generatePDF(Id recordId, String htmlData){
        Pagereference page = Page.renderAsPDF;
        page.getParameters().put('pdfText', htmlData);

        Contact con = new Contact(Id=recordId);
        Attachment objAttachment = new Attachment();
        objAttachment.ParentId = con.Id;
        objAttachment.Body = page.getContentaspdf();
        objAttachment.IsPrivate = false;
        insert objAttachment;
        return objAttachment;
Visualforce Page used to display and create PDF:
<apex:page controller="pdfPageController" renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="true" readOnly="true">
            <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 
        <   apex:outputText value="{!pdfText}" escape="false"/>

Visualforce Page Controller:
public with sharing class pdfPageController {
    public String pdfText{get;set;}

    public pdfPageController() {
        pdfText = String.escapeSingleQuotes(
        system.debug(LoggingLevel.INFO, '!!!!@@@@ IM IN ');

I'm realizing that my visualforce page also is not generating a pdf when I use it in lightning.  THis is  a page that I have used in classic.  If I try to use it in classic, the PDF is generated,  however, if I try to use that same visualforce page in lightning, the second visualforce page that is used to renderAsPdf is never triggered.  So this seems to be an issue in my org for lightning.
Piyush VaddoriaPiyush Vaddoria
put "oauth_token" as a param to the page reference.
page.getParameters().put('oauth_token', userinfo.getSessionId());

This should work.