PDF Download in ABAP

by | Jun 3, 2018 | ABAP Programs

Home » SAP » ABAP » ABAP Programs » PDF Download in ABAP

Preface – This post is part of the ABAP Programs series.

In this article, you will learn how to convert xstring to pdf in abap and how to use GUI_DOWNLOAD Function Module to perform pdf download in ABAP.

Introduction

In our previous article we have uploaded a PDF file. To view it click here. Now we will be Downloading that file into our system. To do that we need to convert the XSTRING file into a PDF file. The program shown below uses Function Module GUI_DOWNLOAD to download the file and Function Module SCMS_XSTRING_TO_BINARY to convert the XSTRING File to a Binary file which is further converted to a PDF file using GUI_DOWNLOAD Function Module.
Program to Download PDF in ABAP

Parameters:
  p_fname type z1127582_upload-filename lower case,
  p_path type string lower case.


Data:
      gs_store_file type z1127582_upload,
      xstr_content type xstring,
      gt_content type standard table of tdline,
      len type i,
      str_fname type string.

Start-of-selection.

  select single * from z1127582_upload
    into gs_store_file
    where filename = p_fname.

  xstr_content  = gs_store_file-file_content.

  "Convert xstring/rawstring to binary ITAB
  call function 'SCMS_XSTRING_TO_BINARY'
    exporting
      buffer        = xstr_content
    importing
      output_length = len
    tables
      binary_tab    = gt_content.
  .

  if sy-subrc <> 0.
    message 'Unable to convert xstring to binary'
      type 'E'.
  endif.

  str_fname = p_fname.

  call function 'GUI_DOWNLOAD'
    exporting
      bin_filesize            = len
      filename                = p_path
      filetype                = 'BIN'
    tables
      data_tab                = gt_content
    exceptions
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      others                  = 22.
  if sy-subrc <> 0.
    message 'Unable to download file from SAP'
      type 'E'.
  endif.

Explanation

Initially, we have defined variables in two parts:

  • Parameter: p_fname is the name of file and p_path stores location of file
  • gs_store_file, gt_content, len, xstr_content: data related to file i.e. where file will be store, how the content will be stored, length of the file and type of content.

Function Module used:

  • SCMS_XSTRING_TO_BINARY
  • GUI_DOWNLOAD

What exactly is stored in the table that will be accessed by the Function Module to perform PDF download in ABAP:

  1. PDF file location
  2. PDF file name
  3. File type
  4. File X-STRING Data [This is the actual file that is converted in x-string]

Later on we have called different Function Modules. We have added respective comment in the code itself.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author