Emaiing pdf not working

Stimulsoft Reports.JS discussion
Post Reply
Doghousedev
Posts: 7
Joined: Thu Oct 11, 2018 10:34 am

Emaiing pdf not working

Post by Doghousedev »

I am trying to attach a pdf (quote.pdf) to JSON body being sent to the mail server.

I am getting a corrupted pdf being sent.

The pdf renders on screen in the viewer properly. If I manually encode the saved rendered report (pdf) as Base64 , hard code it in the JSON message base64Content and send it to the mail server, it attaches correctly.

However, when I try to encode the quote programmatically, I am getting a corrupted pdf. Console Logging the base64data output and comparing it to the same report base64 are different.

I have an onEmailReport which calls sendMail function

Code: Select all

viewer.onEmailReport = () => sendEmail(viewer);

async function sendEmail(viewer) {

  const report = viewer.report

  console.log(report)

  // Export report as PDF 
  const pdf = report.exportDocument(Stimulsoft.Report.StiExportFormat.Pdf);

  // Convert to Base64 string
  const base64data = btoa(pdf)
  console.log(base64data );

  const data = {
    "Messages": [
      {
        "From": {
          "Email": "ron@demomail.com",
          "Name": "Ronald "
        },
        "To": [
          {
            "Email": "doghousedev@demomail.com",
            "Name": "Rogue "
          }
        ],
        "Subject": "Email PDF Quote Testing!",
        "TextPart": "Dear customer find attached quote",
        "HTMLPart": "<h3>Dear Rogue , Here us you quote!",
        "Attachments": [
          {
            "ContentType": "application/pdf",
            "Filename": "quote.pdf",
            "Base64Content": base64data
          }
        ]
      }
    ]
  };

  // Send the email
  try {
    const response = await fetch('MY SERVER.COM', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    });

    if (!response.ok) throw new Error('Email sending failed');
    const responseData = await response.json();
    console.log("Response Data:", responseData);
    // Handle success here
  } catch (error) {
    console.error('Error:', error);
    // Handle error here
  }
};
Doghousedev
Posts: 7
Joined: Thu Oct 11, 2018 10:34 am

Re: Emaiing pdf not working

Post by Doghousedev »

here is the quote which is render and sent via email
Attachments
quote (1).pdf
(818.09 KiB) Downloaded 89 times
HighAley
Posts: 8431
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Emaiing pdf not working

Post by HighAley »

Hello,

The attached file contains a comma-separated list of character codes.

Please, check your code that converts the result of our export.

Thank you.
Doghousedev
Posts: 7
Joined: Thu Oct 11, 2018 10:34 am

Re: Emaiing pdf not working

Post by Doghousedev »

the solution wasn't clearly obvious to me but figured it out with the help of chatgpt.

Code: Select all

/* Function to convert PDF data to base64 */
function convertPdfToBase64(report) {
  return new Promise((resolve, reject) => {
    report.renderAsync(() => {
      report.exportDocumentAsync((pdfData) => {
        const byteArray = new Uint8Array(pdfData);
        const pdfBlob = new Blob([byteArray], { type: 'application/pdf' });
        const reader = new FileReader();

        reader.onloadend = function () {
          const base64data = reader.result.split(',')[1];
          resolve(base64data);
        };

        reader.onerror = reject;

        reader.readAsDataURL(pdfBlob);
      }, Stimulsoft.Report.StiExportFormat.Pdf);
    });
  });
}
Lech Kulikowski
Posts: 6271
Joined: Tue Mar 20, 2018 5:34 am

Re: Emaiing pdf not working

Post by Lech Kulikowski »

Hello,

Thank you for the information.
Post Reply