0

I am struggling to figure out a method to take an HTML page and convert it to a pdf and then turn that pdf into a base64.

Specifically, I am working on making an client-side webpage where a user can fill in information about an invoice and then click a "send email" button this button should take the entire invoice page and convert it to a pdf. I then need to use emailjs to send the pdf to a client. To my understanding emailjs requires attachments to be in base64 form so I think I would need to convert the pdf to base64 in order to attach it.

All of this needs to be done client side and with vanilla js. Any ideas or examples on how I could accomplish this would be greatly appreciated!

codes
  • 21
  • 4
  • this is very broad and sounds more like a project description than a question. Split it up into tasks, see what you can achieve and ask specific questions when you're stuck and can't find the answer. Taking the [tour] and reading [ask] are generally useful for beginners on this site. For example, for base64 encoding on client side you can find answers here: [Base64 encoding and decoding in client-side Javascript](https://stackoverflow.com/q/2820249) and of course by searching on the site. – jps Jul 05 '21 at 15:12

1 Answers1

1

I'd never use Emailjs but maybe that's your solution for your pdf file

const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});

async function Main() {
const file = document.querySelector('#myfile').files[0];
const result = await toBase64(file).catch(e => Error(e));
if(result instanceof Error) {
  console.log('Error: ', result.message);
  return;
}


Main();
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Eduardo GR
  • 26
  • 3