Monday, July 9, 2012
This program is basically converts smartform output to PDF and displays that PDF data in Custom Container .

Steps:
  1. Create one Smartform
  2. Get the OTF from smartform 
  3. Convert the smartform into PDF format using function module  CONVERT_OTF
  4. Load PDF data into HTML control CL_GUI_HTML_VIEWER->LOAD_DATA.
    • Pass PDF data table
    • Import URL
  5. Display PDF using CL_GUI_HTML_VIEWER->SHOW_URL
Example program steps 
  1. Create one module pool program SAPMZPDF_DISPLAY. 
  2. Create screen 1001. Attributes Tab-> Enter Short description - Screen to display PDF. Element List Tab->Define OKCODE. 
  3. Click on LAYOUT button and define Custom Control on Screen. Name it as CUST_CONTROL.

  4. Flow Logic Tab-> Place the below code
    "PBO
    PROCESS BEFORE OUTPUT.
      MODULE status_1001.  "Status
      MODULE get_pdf_data. "Get PDF
      MODULE display_pdf.  "Display PDF
    "PAI
    PROCESS AFTER INPUT.
      MODULE exit AT EXIT-COMMAND.
    
  5.  TOP include contains declarations
    *&---------------------------------------------------------------------*
    *& Include MZPDF_DISPLAYTOP    Module Pool      SAPMZPDF_DISPLAY
    *&
    *&---------------------------------------------------------------------*
    PROGRAM  sapmzpdf_display.
    "Containers
    DATA:
         html_viewer         TYPE REF TO cl_gui_html_viewer,
         main_container      TYPE REF TO cl_gui_custom_container.
    
    TYPES:
          ty_it0002          TYPE pa0002,
          ty_it0008          TYPE pa0008,
          ty_control_par     TYPE ssfctrlop,
          ty_output_options  TYPE ssfcompop,
          ty_job_output_info TYPE ssfcrescl,
          ty_otf_data        TYPE itcoo,
          ty_pdf             TYPE tline.
    DATA:
          wa_it0002          TYPE ty_it0002,
          wa_it0008          TYPE ty_it0008,
          wa_control_par     TYPE ty_control_par,
          wa_output_options  TYPE ty_output_options,
          wa_job_output_info TYPE ty_job_output_info,
          wa_pdf             TYPE ty_pdf.
    DATA:
          it_it0002         TYPE STANDARD TABLE OF ty_it0002,
          it_it0008         TYPE STANDARD TABLE OF ty_it0008,
          it_otf_data       TYPE STANDARD TABLE OF ty_otf_data,
          it_pdf            TYPE STANDARD TABLE OF ty_pdf,
          it_data           TYPE STANDARD TABLE OF x255.
    DATA:
          gv_fm_name        TYPE  rs38l_fnam,
          gv_url            TYPE char255,
          gv_content        TYPE xstring,
          okcode            TYPE sy-ucomm,
          gv_bin_filesize   TYPE i.
    
    FIELD-SYMBOLS  TYPE x.
    
  6. MODULE status_1001.
    *&---------------------------------------------------------------------*
    *&      Module  STATUS_1001  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE status_1001 OUTPUT.
    
      SET PF-STATUS 'STATUS1'.
      SET TITLEBAR 'T1'.
    
    ENDMODULE.                 " STATUS_1001  OUTPUT
    
  7. MODULE get_pdf_data.
    *&---------------------------------------------------------------------*
    *&      Module  GET_PDF_DATA  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE get_pdf_data OUTPUT.
    
      PERFORM get_pdf_data.
    
    ENDMODULE.                 " GET_PDF_DATA  OUTPUT
    
  8. PERFORM get_pdf_data.
    *&---------------------------------------------------------------------*
    *&      Form  GET_PDF_DATA
    *&---------------------------------------------------------------------*
    FORM get_pdf_data .
      IF it_pdf IS INITIAL.
        "Get data from DB
        SELECT *
          FROM pa0002
          INTO TABLE it_it0002
          UP TO 6 ROWS.
        SELECT *
          FROM pa0008
          INTO TABLE it_it0008
          UP TO 6 ROWS.
        "Get smartform function module name
        CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
          EXPORTING
            formname           = 'Z2DISPLAY_N_PDF'
          IMPORTING
            fm_name            = gv_fm_name
          EXCEPTIONS
            no_form            = 1
            no_function_module = 2
            OTHERS             = 3.
        "Pass Printer related Parameters to Smartform
        wa_control_par-no_dialog = 'X'.     " It supresses the Printer dialog.
        wa_control_par-getotf    = 'X'.     " Get OTF data
        wa_output_options-tddest = 'LOCAL'. " Set printer
        "Call Smartform
        CALL FUNCTION gv_fm_name
          EXPORTING
            control_parameters = wa_control_par
            output_options     = wa_output_options
          IMPORTING
            job_output_info    = wa_job_output_info
          TABLES
            tab_it0002         = it_it0002
            tab_it0008         = it_it0008
          EXCEPTIONS
            formatting_error   = 1
            internal_error     = 2
            send_error         = 3
            user_canceled      = 4
            OTHERS             = 5.
        IF sy-subrc <> 0.
        ELSE .
          it_otf_data = wa_job_output_info-otfdata.
          "Convert OTF data to PDF
          CALL FUNCTION 'CONVERT_OTF'
            EXPORTING
              format                = 'PDF'
            IMPORTING
              bin_filesize          = gv_bin_filesize
            TABLES
              otf                   = it_otf_data
              lines                 = it_pdf
            EXCEPTIONS
              err_max_linewidth     = 1
              err_format            = 2
              err_conv_not_possible = 3
              err_bad_otf           = 4
              OTHERS                = 5.
        ENDIF.
      ENDIF.
    ENDFORM.                    " GET_PDF_DATA
    
  9. MODULE display_pdf.
    *&---------------------------------------------------------------------*
    *&      Module  DISPLAY_PDF  OUTPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE display_pdf OUTPUT.
      "create the controls and their containers
      IF main_container IS INITIAL.
        CREATE OBJECT main_container
          EXPORTING
            container_name              = 'CUST_CONTROL'
          EXCEPTIONS
            cntl_error                  = 1
            cntl_system_error           = 2
            create_error                = 3
            lifetime_error              = 4
            lifetime_dynpro_dynpro_link = 5
            OTHERS                      = 6.
        "convert pdf to xstring string
        LOOP AT it_pdf INTO wa_pdf.
          ASSIGN wa_pdf TO  CASTING.
          CONCATENATE gv_content  INTO gv_content IN BYTE MODE.
        ENDLOOP.
    
        "create PDF Viewer object
        CREATE OBJECT html_viewer
          EXPORTING
            parent = main_container.
        IF sy-subrc <> 0.
          "NO PDF viewwer
        ENDIF.
        "Convert xstring to binary table to pass to the LOAD_DATA method
        CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
          EXPORTING
            buffer     = gv_content
          TABLES
            binary_tab = it_data.
        " Load the HTML
        CALL METHOD html_viewer->load_data(
           EXPORTING
             type         = 'application'
             subtype      = 'pdf'
           IMPORTING
             assigned_url         = gv_url
           CHANGING
             data_table           = it_data
           EXCEPTIONS
             dp_invalid_parameter = 1
             dp_error_general     = 2
             cntl_error           = 3
             OTHERS               = 4 ).
        IF sy-subrc <> 0.
          WRITE:/ 'ERROR: CONTROL->LOAD_DATA'.
          EXIT.
        ENDIF.
        "Show it
        CALL METHOD html_viewer->show_url( url = gv_url in_place = 'X').
        IF sy-subrc <> 0.
          WRITE:/ 'ERROR: CONTROL->SHOW_DATA'.
          EXIT.
        ENDIF.
      ENDIF.
    ENDMODULE.                 " DISPLAY_PDF  OUTPUT
    
  10. MODULE exit AT EXIT-COMMAND.
    *&---------------------------------------------------------------------*
    *&      Module  EXIT  INPUT
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    MODULE exit INPUT.
      CASE okcode.
        WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
    
          CALL METHOD html_viewer->free.
          IF sy-subrc <> 0.
            WRITE:/ 'CONTROL ERROR'.
            EXIT.
          ENDIF.
          CALL METHOD main_container->free.
          IF sy-subrc <> 0.
            WRITE:/ 'CONTROL ERROR'.
            EXIT.
          ENDIF.
          CALL METHOD cl_gui_cfw=>flush.
          IF sy-subrc <> 0.
            WRITE:/ 'CONTROL ERROR'.
            EXIT.
          ENDIF.
          LEAVE PROGRAM.
      ENDCASE.
    ENDMODULE.                 " EXIT  INPUT
    

5 comments:

  1. Thank you so much for providing such a useful information about SAP Consulting.

    ReplyDelete
  2. Thank you very much for useful info and i implemented code as above it is working fine but now i have a issue like i created alv grid in grid i selected first record its displaying perfectly but if i select next record and print it is showing old form only please help me.
    thank you,
    KK

    ReplyDelete
  3. Hi,
    this is karthick. implemented code as above it is working fine. But i want to disable print and Save Button, Plz help me

    ReplyDelete

  4. Excellent information with unique content and it is very useful to know about the information based on blogs.


    Hadoop online Training

    ReplyDelete

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.