ABAP code to Print abap web dynpro table

Once you have created a basic table on your web dynpro for ABAP application your users might want to be able to print this table, and not just the records currently displayed but all the records in the table. With the ABAP code below this becomes a fairly simply process! This will print the table to the users SAP printer or any printer setup in SAP.

An alternative method would be to call a SAP BSP and display the data using HTML statements, this would allow the user to print the information via there web browser or copy it into an external application such as excel.


Step 1 - Create Print button within your abap web dynpro application
First of all add a print button to you web dynpro for ABAP application and assign an Action to it.



Step 2 - Retrieve data displayed to user within ABAP web dynpro table
In-order to retreive the data displayed to a user within an abap web dypro table you you need to either follow the steps within the Read ABAP Web Dynpro Table section, which shows you how to get the data contained within an wdp table context, or simply re-retrieve the data from the SAP database using standard ABAP code such as:

* Retrieve new data
  select *
    from scarr
    into table it_scarr
   where CARRID = 'AA' .


Step 3 - Insert the Print code
The next step is to add the ABAP code which takes the internal table of data and sends it to the printer. It does this by using the print functionality of the object ALV method, so builds an ALV grid based on your internal table and then sends this to your SAP printer. For the example below the internal table containing the data is IT_SCARR, this gets assigned to it_datatab which in turn get assign to the field symbol <tab>. This means that it does not matter what structure your data table has, the code will build the printout appropriately. Therefore if you are using a data table with a differnet name and/or structure all you need to do is replace IT_SCARR with the name of your declared itab.

* Print web dynpro table using ALV object print functionality
  data it_datatab type ref to data.
* assign table containing data to it_datatab
  get reference of it_scarr into it_datatab.
  FIELD-SYMBOLS: <tab> TYPE table.
  ASSIGN it_datatab->* TO <tab>. "assign data table to field symbol
  DATA: it_alvtable   TYPE REF TO cl_salv_table.
  DATA: ld_prnt_params TYPE pri_params,
        ld_valid(1) TYPE c.
* Get print parameters
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      copies                 = '1'  "print_options-copies
      layout                 = 'X_65_255'
      no_dialog              = abap_true
    IMPORTING
      out_parameters         = ld_prnt_params
      valid                  = ld_valid
    EXCEPTIONS
      archive_info_not_found = 1
      invalid_print_params   = 2
      invalid_archive_params = 3
      OTHERS                 = 4.
  IF sy-subrc ne 0 OR ld_valid NE abap_true.
* invalid print parameters
    return.
  ENDIF.
* Start List Processing using generated print parameters
  NEW-PAGE PRINT ON PARAMETERS ld_prnt_params NO DIALOG.
* If you insert ABAP write statements here they will apear on your printout
* write: 'HelloWorld'.
* Create the ALV Object
  DATA: error_string TYPE string.
  cl_salv_table=>factory(
    EXPORTING
      list_display = abap_true
    IMPORTING
      r_salv_table = it_alvtable
    CHANGING
      t_table      =  ).
* Process the ALV Columns
  DATA: columns TYPE REF TO cl_salv_columns_table.
* Reference ALV columns object
  columns = it_alvtable->get_columns( ).
* Set columns to optimised width.
  columns->set_optimize( abap_false ).
*Set ALV to print in the background
  it_alvtable->display( ).
  NEW-PAGE PRINT OFF.
* Display print complete message

Related Articles

Add programmed/OVS search help to FPM ESS/MSS application via enhancement to CL_HRESS_PER_DETAIL
BIND_STRUCTURE method of interface IF_WD_CONTEXT_NODE to assign structure to context
BIND_TABLE method of interface IF_WD_CONTEXT_NODE
Get the name of the button the user has clicked on within the SAP web dynpro action ABAP code
Create field Refering to datatype of webDynpro context element
WebDynpro context for creation of screen field including standard search help
Disable SAP User Personalisation in Web Dynpro app using WDDISABLEUSERPERSONALIZATION and WDENABLEUIELEMENTSHIDE
GET_ATTRIBUTE method of interface IF_WD_CONTEXT_NODE to retrieve value of context element
GET_SELECTED_ELEMENTS to get selected row of ABAP web dynpro table when not using lead selection
GET_STATIC_ATTRIBUTE_TABLE method to return all rows of a context table node
IF_WD_CONTEXT_NODE interface methods withn ABAP Web Dynpro to enable users to view and change data
ABAP code to manually trigger ABAP web dynpro plug
Popuate SAP WebDynpro field
Freely Programmed search help for your web dynpro application (custom value help/OVS)
Change the stylesheet theme used by your web dynpro and portal apps (i.e. SAP_TRADESHOW, SAP_CORBU)
Change the stylesheet theme used by your web dynpro and portal apps (i.e. SAP_TRADESHOW, SAP_CORBU)
SET_ATTRIBUTE method of interface IF_WD_CONTEXT_NODE to assign value to individuale context attribute
Clear context attribute using SET_ATTRIBUTE_NULL method of interface IF_WD_CONTEXT_NODE
Add Dropdown by index UI Element to table field within abap web dynpro
Add freely Programmed search help to a web dynpro application field
Retrieve SAP webDynpro field value entered by user
ABAP Web dynpro ALV report table
Hide fields of a web dynpro ALV table
Display web dynpro ALV report select options to resict output results
Assign value to ABAP web dynpro context via table, structure or individual attribute
Get selected ABAP Web dynpro dropdown byindex value (dropdownbyindex UI element)
ABAP web dynpro Dropdown by index UI Element allows a field to display a drop down list of values
ABAP web dynpro Dropdown default value, allow you to set current value of dropdown UI element
Change SAP web dynpro logon screen
Read ABAP Web dynpro table context on action / button click
ABAP Web Dynpro tables to allow users to view and change data
Get selected row of ABAP web dynpro table
Get all selected rows of table within your web dynpro for ABAP applications using GET_SELECTED_ELEMENTS
Get selected row of ABAP web dynpro table
ABAP Web Dynpro tree structure, display HR org structure as example
SAP ABAP Web Dynpro UI elements
View Container UI Element to display a web dynpro view within a wdp view
ALV ABAP Web Dynpro application
ABAP Web dynpro button click / Action
Call URL from SAP Webdynpro for ABAP ACTION
Call URL from SAP Webdynpro for ABAP ACTION
Display popup screen within ABAP Web dynpro
ABAP Web dynpro populate table on action / button click
Adding URL parameters to you ABAP webdynpro to change thinks like the CSS