April 26, 2020

How to print a Lightning Component with record data - PART 2

Hello Trailblazers !

In this blog post, we are going to see how to print Lightning Component and how we can hide some elements while printing using standard slds class.

.slds-no-print selector is used to hide a component when printing a page.

Example:
  1. <div class="slds-no-print">hide this div when printing</div>



  1. //AccountController.apxc
  2. public class AccountController {
  3. @AuraEnabled
  4. public static String getAccountName(String accId){
  5. String accName;
  6. if(accId != null){
  7. accName = [SELECT id, Name FROM Account WHERE id= :accId].Name;
  8. System.debug('accName'+accName);
  9. return accName;
  10. }else{
  11. return null;
  12. }
  13. }
  14. }
  15.  

Step 1 : Create Lightning Component that you want to print

  1. <!-- AccountComponent.cmp-->
  2. <aura:component controller="AccountController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  3. <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
  4. <aura:attribute name="accName" type="String"/>
  5. <lightning:button class="slds-no-print slds-align_absolute-center slds-m-around_xx-small"><a style="text-decoration: none;" href="{!'/c/PrintComponent.app?recId='+v.recordId}" target="_blank" class="">Print Preview</a></lightning:button>
  6. <lightning:icon iconName="utility:company" alternativeText="Account" title="Account Name"/>{!v.accName}
  7. </aura:component>

  1. /* AccountComponentController.js */
  2. ({
  3. doInit : function(component, event, helper) {
  4. //To retrive account name
  5. var action = component.get("c.getAccountName");
  6. var accRecordId = component.get("v.recordId");
  7. action.setParams({
  8. "accId": accRecordId
  9. });
  10. action.setCallback(this,function(response){
  11. var state = response.getState();
  12. if (state === "SUCCESS") {
  13. var aName = response.getReturnValue();
  14. component.set("v.accName",aName);
  15. }else if (state === "INCOMPLETE") {
  16. console.log("Incomplete");
  17. }else if (state === "ERROR") {
  18. var errors = response.getError();
  19. if (errors) {
  20. if (errors[0] && errors[0].message) {
  21. console.log("Error message: " + errors[0].message);
  22. }
  23. }else {
  24. console.log("Unknown error");
  25. }
  26. }
  27. });
  28. $A.enqueueAction(action);
  29. }
  30. })

Step 2 : Create Lightning App to call lightning component in it.

  1. <!-- PrintComponent.app-->
  2. <aura:application extends="force:slds" access="global">
  3. <ltng:require scripts="" afterScriptsLoaded="{!c.scriptsLoaded}" />
  4. <aura:attribute name="recId" type="String" />
  5. <aura:attribute name="isLoaded" type="Boolean" default="false"/>
  6. <aura:if isTrue="{!v.isLoaded}">
  7. <div class="printCMP">
  8. <c:AccountComponent recordId="{!v.recId}"/>
  9. </div>
  10. </aura:if>
  11. </aura:application>

Step 3: Use window.print() inside js function

  1. /* PrintComponentController.js */
  2. ({
  3. scriptsLoaded : function(component, event, helper) {
  4. component.set("v.isLoaded", true);
  5. setTimeout(function(){
  6. window.print();
  7. }, 2000);
  8. }
  9. })

Step 4 : Use @media print in css

  1. <!-- PrintComponent.css-->
  2. @media print {
  3. .THIS.printCMP {
  4. background-color: white;
  5. height: 100%;
  6. width: 100%;
  7. position: fixed;
  8. top: 0;
  9. left: 0;
  10. margin: 0;
  11. padding: 15px;
  12. font-size: 14px;
  13. line-height: 18px;
  14. }
  15. }


Resources



No comments:

Post a Comment