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
Tyler Tran 94Tyler Tran 94 

Generate and download PDF file from Base64 Encode String

Hello everyone! I want to create a LWC to download a simple PDF file with a random string. Here is my code:
Apex class:
public with sharing class PdfLwcController {
    @AuraEnabled
    public static String getBase64String() {
        String s = 'Hello World!';
        
        return EncodingUtil.base64Encode(Blob.toPdf(s));
    }
}

Client-side controller:
import { LightningElement } from 'lwc';
import getBase64String from '@salesforce/apex/PdfLwcController.getBase64String';

export default class PdfButton extends LightningElement {
    handleClick() {
        getBase64String({})
            .then(response =>{
                // I don't know how to generate and download pdf file.
            })
    }
}

HTML:
<template>
    <lightning-card title="Downloading Pdf Demo">
        <lightning-button label="Neutral" title="Non-primary action" onclick={handleClick} class="slds-m-left_x-small"></lightning-button>
    </lightning-card>
</template>
The Server-side controller can return a Base64 String, but I don't know how to generate the PDF file and download it immediately (don't save it to the attachment or file). Do you have any idea?
Thank you so much.