Goal:
Hi, First of all let me tell you that I am new to Retool, I am using an external API which fetch the invoice in HTML format, so I want the response of Invoice query to be converted into PDF format and download it when I run the JavaScript code.
I tried using different suggestions but looks like there is not success. Can someone please help me to know if this is possible in retool to "Convert the response of the from HTML to pdf and download the PDF file" ? If, yes how to do it
Hi @Shakeel_Ahmed, there are a number of ways to accomplish this. Here's one:
If you're fetching your invoice from an external API, you can use library like turndown JS to convert the HTML to Markdown, then use Retool's PDF Exporter resource.
Steps:
Settings > Libraries > Add TurndownJS
Use the turndown library in a JS query
// Step 1: Get your HTML content (assuming you have it from an API response)
var htmlContent = query1.data.htmlBlock; // Replace this with your actual HTML content
// Step 2: Create a new instance of Turndown
var turndownService = new TurndownService();
// Step 3: Convert HTML to Markdown
var markdown = turndownService.turndown(htmlContent);
// Step 4: Display or use the Markdown content
return markdown;
Create a PDF Exporter Resource and reference the markdown returned in the previous step
Hi @mroknich Thank you so much, it is really nice help. I succeed to export the PDF now but the problem now is the layout of the PDF is changed. My Invoice is one page only while the retool PDF exporter converted the data into 4 pages which means it doesn't look like a invoice.
Here I am sharing the screenshot of my invoice of A4 size PDF and the screenshot from retool exported PDF.
Can you please help me to know how to export the PDF into original layout ? I want to export the PDF in the same view as it is showing in the screenshot invoice.png
For downloading TAX INVOICE, use the following JavaScript code to generate and download the invoice.
dom-to-image : <script src="https://cdnjs.cloudflare.com/ajax/libs/dom-to-image/2.6.0/dom-to-image.min.js"></script>
pdf-library: <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf-lib/1.17.1/pdf-lib.min.js"></script>
Hi @ZeroCodez Something like this ?
const payslipElement = Invoice.data.d.HTMLInvoice;
or
const payslipElement = document.getElementById(Invoice.data.d.HTMLInvoice);
I tried like this but it is not downloading any PDF, while the Code runs successfully without any error in console.
BTW, I was looking something simpler explained by @mroknich ,
Hi @ZeroCodez ,
Here is the path of the API "Invoice" response: Invoice.data.d.HTMLInvoice (This returns the HTML Data)
Can you please help me to understand how exactly I have to set this in your suggested code ?