AL icon indicating copy to clipboard operation
AL copied to clipboard

Report.SaveAs outstream issue

Open MrDavid5465 opened this issue 9 years ago • 1 comments

I am trying to save a pdf report into an outstream for a blob so that I can convert it to a base64 string to be included in an API call, but when Report.SaveAs is executed, the blob column in the Tempblob table is empty. What might I be doing wrong?

LOCAL PROCEDURE SaveReportAsPDF(ReportID : Integer;LayoutCode : Code[20]) Attachment1 : Text;
  var
    ReportLayoutSelection : Record 9651;
    ReportOutStream : OutStream;
    Tempblob : Record TempBlob;
    myAttachment : InStream;
  begin
    if not Tempblob.IsEmpty then begin
      Tempblob.DeleteAll;
      Commit;
    end;
    
    
    Tempblob.Blob.CreateOutStream(ReportOutStream);
    ReportLayoutSelection.SetTempLayoutSelected(LayoutCode);    
    REPORT.SaveAs(ReportID, Report.RunRequestPage(ReportID), ReportFormat::Pdf, ReportOutStream);
    Tempblob.Blob.CreateInStream(myAttachment);
    ReportLayoutSelection.SetTempLayoutSelected('');
    
    Tempblob.Insert;
    attachment1 := Tempblob.ToBase64String;
  end;

MrDavid5465 avatar Jul 07 '17 18:07 MrDavid5465

I'm note sure this is a bug. Tested it with some code and I do get data in the TempBlob.

This is an example that works:

    local procedure SaveReportAsPdf()
    var
        TempBlob : Record TempBlob temporary;
        OutStr : OutStream;
        RecRef : RecordRef;
        Base64EncodedString : Text;
    begin
        RecRef.GetTable(Rec);
        TempBlob.Blob.CreateOutStream(OutStr);

        Report.SaveAs(Report::"Customer - Sales List",'',ReportFormat::Pdf,OutStr,RecRef);
        
        Base64EncodedString := TempBlob.ToBase64String();
        Message(Base64EncodedString);
    end;

Some remarks about your code example:

  • Always use TempBlob as a temporary table (no need to do deleteall, it will be empty)
  • No insert is needed in the TempBlob table
  • The InStream is never used, it can be removed

ajkauffmann avatar Jul 18 '17 22:07 ajkauffmann