Skip to content

This Salesforce LWC component allows you to create an action button on a Salesforce record that provides the ability to preview a PDF document with save functionality.

Notifications You must be signed in to change notification settings

brbjr1/LWC-PDF-Reader-with-Save

Repository files navigation

Description: This Salesforce LWC component allows you to create an action button on a record that provides the ability to preview a PDF document with save functionality.

alt text

Github: https://github.com/brbjr1/LWC-PDF-Reader-with-Save

Deployment instructions:

  1. Install package
  1. Create a visualforce page to generate the PDF document.
  • Example:
    • Name: AccountReport
    • Code:
<apex:page standardcontroller="Account"
           title="Test Account Report"
           lightningstylesheets="false"
           renderAs="pdf"
           applybodytag="false">
    <!--applybodytag="false" is used to make sure that styles are correctly applied to the generated pdf-->
    <!--standardcontroller="Account" allows getting data from the account object without writing a class-->       
    <head>
        <style type="text/css">
            .rpt * {
                padding: 0;
                margin: 0;
            }
            .rpt-page {
                font: 9pt 'Open Sans', Arial, sans-serif;
                line-height: 1.4em;
            }
            .rpt-page-header {
                overflow: auto;
            }
            .rpt-page-header .rpt-header {
                    width: auto;
                    margin: 0;
            }
            .rpt-page {
                width: 7.4in;
                overflow: hidden;
                margin: .5in;
            }
        </style>
    </head>

    <body>
        <div class="rpt">
            <div class="rpt-page">
                <div class="rpt-header">
                    My Test Report
                </div>

                <table>
                    <tbody>
                        <tr>
                            <td>Name:</td>
                            <td>
                                <apex:outputText value="{!Account.Name}"/>
                                
                            </td>
                        </tr>
                        <tr>
                            <td>Create Date:</td>
                            <td>
                                <apex:outputText value="{0, date, MM'/'dd'/'yyyy}">
                                    <apex:param value="{!Account.CreatedDate}" />
                                </apex:outputtext>
                            </td>
                        </tr>
                    </tbody>
                </table>

            <!--End rpt-page-->
            </div>
        <!--End rpt-->
        </div>
    </body>
</apex:page>
  1. Create a Lightning Component (aura component, at the time of this writing quick actions are not possible to create on LWC).
  • Open the Salesforce development console and click on File => New => Lightning Component
    alt text
  • Enter a name (this is going to be used by our record action button)
  • Check Lightning Record Page and Lightning Quick Action alt text
  • Update the Component markup to:
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
	<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
	<c:lwcpdfReaderWithSave aura:id="my-viewer" onclose="{!c.handleClose}" showConsoleLogs="false" recordId="{!v.recordId}" />
	<aura:attribute name="isLoading" type="Boolean" default="true" />
	<aura:if isTrue="{!v.isLoading}">
		<div>
			<lightning:spinner alternativeText="Loading" size="small" />
		</div>
	</aura:if> 
</aura:component>

alt text

  1. Update the Controller (Modify line “myviewer.doOpen({'documentsaveFormulaField':'','documentsaveName':'Document.pdf','VFReportPageName':'AccountReport','modalTitle':'Account Report'});” for your report)
  • Configuration Options:
    • documentsaveFormulaField: Used to dynamically create a document name for the saved report. The document name is queried from the record you are generating the report from. This value needs to be the field API name. An example, on the sample account report if you enter Name the saved file name will equal the accounts name with the extension .pdf or using a custom formula field would allow adding dates or other attributues to the name. (enter a blank value to disable)
    • documentsaveName: Enter a static name to save the document as. (enter a blank value if using documentsaveFormulaField)
    • VFReportPageName: Enter the name of the visualforce page that generates the pdf.
    • modalTitle: Enter the name that displays on the top of the preview modal.
({
	doInit : function(component, event, helper) {
        window.setTimeout(
            $A.getCallback(function() {
				let myviewer = component.find("my-viewer");
				myviewer.doOpen({'documentsaveFormulaField':'','documentsaveName':'Document.pdf','VFReportPageName':'AccountReport','modalTitle':'Account Report'});
				component.set("v.isLoading", false);
			}), 0
        );
    },
	handleClose : function(component, event, helper) {
		$A.get("e.force:closeQuickAction").fire();  
	}

})

alt text

  1. Create an action button on the target object and add the button to the target page layout alt text alt text alt text

  2. Test

About

This Salesforce LWC component allows you to create an action button on a Salesforce record that provides the ability to preview a PDF document with save functionality.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published