Saturday, April 20, 2019

Report.SaveAsPDF in Business Central (Workaround) - Download Report PDF in Business Central

Hello Friends,

As we know, Report.SaveAsPDF or Report.SaveAsExcel not supported in Business Central Cloud (SaaS) version.

We have an alternate code for solution. We can save Report in Stream objects than after convert to file using Blob Export function.

Here is the sample code to download work order report in PDF file with Customize file Name.


pageextension 50212 SalesOrderEx extends "Sales Order"
{
    layout
    {
        // Add changes to page layout here
    }

    actions
    {
        // Add changes to page actions here
        addlast("&Print")
        {
            action("Download Report")
            {
                ApplicationArea = All;
                Image = ExportFile;
                trigger OnAction()
                var
                    TempBlob_lRec: Record TempBlob temporary;
                    Out: OutStream;
                    RecRef: RecordRef;
                    FileManagement_lCdu: Codeunit "File Management";
                    SalesHeader_lRec: Record "Sales Header";
                begin
                    TempBlob_lRec.Blob.CreateOutStream(Out, TEXTENCODING::UTF8);
                    SalesHeader_lRec.Reset;
                    SalesHeader_lRec.SetRange("Document Type", Rec."Document Type");
                    SalesHeader_lRec.SetRange("No.", Rec."No.");
                    SalesHeader_lRec.FindFirst();
                    RecRef.GetTable(SalesHeader_lRec);
                    REPORT.SAVEAS(752, '', REPORTFORMAT::Pdf, Out, RecRef);
                    FileManagement_lCdu.BLOBExport(TempBlob_lRec, STRSUBSTNO('SalesOrder_%1.Pdf', "No."), TRUE);
                end;

            }
        }
    }


Hope this help someone to get solution of download pdf file in business central

Thank you for Reading...

Keep Sharing...Keep Growing....

16 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,
    The TempBlob Record is no more in D 365BC wave2, So how we use Codeunit "Temp blob" please suggest on this to download PDF

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks for this, i'm having issue with converting the blob to byte. Converting to byte is my main goal, because i want to use it on HttpClient.
    The file is always corrupt

    When i tried to save it into file and open up the pdf, i couldn't even open it up. Please what i'm i doing wrong

    ReportID := GetDocumentReportID(DocumentVariant);
    DataTypeManagement.GetRecordRef(DocumentVariant,DocumentRef);
    TempBlob.Blob.CREATEOUTSTREAM(VarOutStream);
    REPORT.SAVEAS(ReportID,'',REPORTFORMAT::Pdf,VarOutStream,DocumentRef);

    TempBlob.Blob.CREATEINSTREAM(VarInStream);

    MemStream := MemStream.MemoryStream();
    COPYSTREAM(MemStream,VarInStream);
    bytes := MemStream.ToArray();

    File.TEXTMODE(TRUE);
    File.WRITEMODE(TRUE);
    File.OPEN('C:\temp\test.pdf');
    File.WRITE(Convert.ToBase64String(bytes));
    File.CLOSE;

    ReplyDelete
  5. Terrific article! This is the type of information that should be shared across the web.

    ba 2nd semester result vikram university

    ReplyDelete
  6. Hi Nilesh, thank you for your article, it is very useful!

    We have to do the same but downloading more than one report (in our case we have invoices).
    We are doing the same as you in a proccesing only report, we write your code in the OnAfterGetRecord trigger in "Sales Invoice Header" dataitem. In the request page we set an interval of Document No. (f.e: 7033..7035) and when we do the process we only received the last document in the interval (7035).
    Could you help us?, Do you know what we could do to receive a pdf of each document?

    Thank you for your time.

    Alba Rey

    ReplyDelete
    Replies
    1. Hi Alba,
      We are having the same problem. Did you come up with a solution?
      Thanks for your help!

      Delete
  7. want to download report from API ,direct pdf can you please suggest

    ReplyDelete