I managed to create my own pdf receipt using pdf-lib and my top co-worker chat-GPT. A few things to know;
The instance name is PDFLib.
Ask chat-GPT to do write small sections at a time.
If your wanting to print straight away on a button press this is how you can do it.
createReceipt().then((pdfBytes) => {
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
// Create a Blob URL for the Blob
const pdfBlobUrl = URL.createObjectURL(blob);
// Create a hidden iframe element
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = pdfBlobUrl;
// Append the iframe to the document body
document.body.appendChild(iframe);
// Wait for the PDF to load in the iframe
iframe.onload = function() {
// Call print on the iframe's content window
iframe.contentWindow.print();
// Optionally, remove the iframe after printing
setTimeout(function() {
document.body.removeChild(iframe);
}, 2000);
};
});
Here’s PDF-LIB’s documentation https://pdf-lib.js.org/
Hope this helps,
Good Luck!