Save report as pdf and send as e-mail

Hi experts,

On Business Central cloud, I have made my own report in my own extension.

I need to add an option for the user to select, if the report should be printed as normal or if it should be sent as an e-mail with a pdf document attched.

I don't want the user to select, where the pdf document should be saved during the process or what file name it should have. When the user selects "Send report as e-mail" on the request page of the report, the e-mail program should pop up with the pdf document attached to a new e-mail, that hasn't been sent yet. Then the user can enter some text and send it manually.

After that the pdf document should only be in the e-mail and not on any disc.

How do I do that?

Best Answer

  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @MortenSteengaard ,

    Here is an example of how to save a report to PDF and then show the email compose page.
    pageextension 50120 CustListExtEmail extends "Customer List"
    {
        actions
        {
            addafter("&Customer")
            {
                action(EmailEdit)
                {
                    Promoted = true;
                    PromotedCategory = Process;
                    PromotedIsBig = true;
                    PromotedOnly = true;
                    Image = Email;
                    ApplicationArea = All;
                    trigger OnAction()
                    begin
                        EmailEdit(Rec."No.");
                    end;
                }
            }
        }
    
        local procedure EmailEdit(CustomerId: Code[20]): Text
        var
            tmpBlob: Codeunit "Temp Blob";
            cnv64: Codeunit "Base64 Convert";
            InStr: InStream;
            OutStr: OutStream;
            txtB64: Text;
            format: ReportFormat;
            email: Codeunit Email;
            emailMsg: Codeunit "Email Message";
            recRef: RecordRef;
            Customer: Record Customer;
    
        begin
            Customer.Get(CustomerId);
            Customer.SetRange("No.", CustomerId);
            recRef.GetTable(Customer);
            tmpBlob.CreateOutStream(OutStr);
            if Report.SaveAs(Report::"Customer - Order Summary", '', format::Pdf, OutStr, recRef) then begin
                tmpBlob.CreateInStream(InStr);
                txtB64 := cnv64.ToBase64(InStr, true);
                emailMsg.Create('', '', '');
                emailMsg.AddAttachment('OrderSummary.pdf', 'application/pdf', txtB64);
                email.OpenInEditor(emailMsg);
            end;
        end;
    
    }
    

    Regards.

Answers

  • MortenSteengaardMortenSteengaard Member Posts: 136
    Hi ftornero,

    Thanks, but I need help to develop changes to my report.

    Best regards,

    Morten
  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @MortenSteengaard ,

    Here is an example of how to save a report to PDF and then show the email compose page.
    pageextension 50120 CustListExtEmail extends "Customer List"
    {
        actions
        {
            addafter("&Customer")
            {
                action(EmailEdit)
                {
                    Promoted = true;
                    PromotedCategory = Process;
                    PromotedIsBig = true;
                    PromotedOnly = true;
                    Image = Email;
                    ApplicationArea = All;
                    trigger OnAction()
                    begin
                        EmailEdit(Rec."No.");
                    end;
                }
            }
        }
    
        local procedure EmailEdit(CustomerId: Code[20]): Text
        var
            tmpBlob: Codeunit "Temp Blob";
            cnv64: Codeunit "Base64 Convert";
            InStr: InStream;
            OutStr: OutStream;
            txtB64: Text;
            format: ReportFormat;
            email: Codeunit Email;
            emailMsg: Codeunit "Email Message";
            recRef: RecordRef;
            Customer: Record Customer;
    
        begin
            Customer.Get(CustomerId);
            Customer.SetRange("No.", CustomerId);
            recRef.GetTable(Customer);
            tmpBlob.CreateOutStream(OutStr);
            if Report.SaveAs(Report::"Customer - Order Summary", '', format::Pdf, OutStr, recRef) then begin
                tmpBlob.CreateInStream(InStr);
                txtB64 := cnv64.ToBase64(InStr, true);
                emailMsg.Create('', '', '');
                emailMsg.AddAttachment('OrderSummary.pdf', 'application/pdf', txtB64);
                email.OpenInEditor(emailMsg);
            end;
        end;
    
    }
    

    Regards.
  • MortenSteengaardMortenSteengaard Member Posts: 136
    Hi ftornero,

    Thank you very much! That is great!

    Best regards,

    Morten
Sign In or Register to comment.