Conteúdo

Gerar PDF do spool (OTF e ABAPLIST)

Sabe aquele Spool gerado que ficaria lindo dentro de um programa abap? Caraca, isso é possível se você gerar um PDF deste spool. Se liga na lógica:
REPORT ZSAPeiros.
PARAMETERS:
  p_spool1              TYPE          tsp01-rqident,
  p_spool2              TYPE          tsp01-rqident.

DATA:
  tab1                  TYPE TABLE OF soli,
  tab2                  TYPE TABLE OF soli,
  tab1xls               TYPE TABLE OF soli,
  wa_tab1xls            TYPE          soli,
  gt_pdf                TYPE TABLE OF tline,
  gt_pdf1               TYPE TABLE OF tline,
  gt_pdf2               TYPE TABLE OF tline,
  wa_pdf1               TYPE          tline,
  wa_pdf2               TYPE          tline,
  lo_docking_container1 TYPE REF TO   cl_gui_docking_container,
  lo_docking_container2 TYPE REF TO   cl_gui_docking_container,
  lo_html               TYPE REF TO   cl_gui_html_viewer,
  lo_html2              TYPE REF TO   cl_gui_html_viewer,
  lt_data               TYPE STANDARD TABLE OF x255,
  lt_data2              TYPE STANDARD TABLE OF x255,
  lv_url                TYPE          char255,
  lv_url2               TYPE          char255,
  lv_content            TYPE          xstring,
  lv_content2           TYPE          xstring.

FIELD-SYMBOLS:
  <fs_pdf1>             TYPE x,
  <fs_pdf2>             TYPE x.

INITIALIZATION.
  %_p_spool1_%_app_%-text = 'Spool - ABAPLIST'.
  %_p_spool2_%_app_%-text = 'Spool - OTF'.

END-OF-SELECTION.
* Converter ABAPLIST para PDF
  IF p_spool1 IS NOT INITIAL.
    CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid = p_spool1
      TABLES
        pdf         = gt_pdf1.
  ENDIF.

* Converter OTF para PDF
  IF p_spool2 IS NOT INITIAL.
    CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid = p_spool2
      TABLES
        pdf         = gt_pdf2.
  ENDIF.

* Converter PDF em xstring string ABAPLIST
  IF lv_content IS NOT INITIAL.
    LOOP AT gt_pdf1 INTO wa_pdf1.
      ASSIGN wa_pdf1 TO <fs_pdf1> CASTING.
      CONCATENATE lv_content <fs_pdf1> INTO lv_content IN BYTE MODE.
    ENDLOOP.
  ENDIF.

* Converter PDF em xstring string OTF
  IF lv_content2 IS NOT INITIAL.
    LOOP AT gt_pdf2 INTO wa_pdf2.
      ASSIGN wa_pdf2 TO <fs_pdf2> CASTING.
      CONCATENATE lv_content2 <fs_pdf2> INTO lv_content2 IN BYTE MODE.
    ENDLOOP.
  ENDIF.

* Container ABAPLIST
  IF lv_content IS NOT INITIAL.
    CREATE OBJECT lo_docking_container1
      EXPORTING
        repid     = sy-repid
        dynnr     = sy-dynnr
        side      = lo_docking_container1->dock_at_right
        extension = 700.
  ENDIF.

* Container OTF
  IF lv_content2 IS NOT INITIAL.
    CREATE OBJECT lo_docking_container2
      EXPORTING
        repid     = sy-repid
        dynnr     = sy-dynnr
        side      = lo_docking_container2->dock_at_left
        extension = 700.
  ENDIF.

* HTML ABAPLIST
  IF lv_content IS NOT INITIAL.
    CREATE OBJECT lo_html
      EXPORTING
        parent = lo_docking_container1.
  ENDIF.

* HTML OTF
  IF lv_content2 IS NOT INITIAL.
    CREATE OBJECT lo_html2
      EXPORTING
        parent = lo_docking_container2.
  ENDIF.

* Converter xstring em binary table ABAPLIST
  IF lv_content IS NOT INITIAL.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer     = lv_content
      TABLES
        binary_tab = lt_data.
  ENDIF.

* Converter xstring em binary table OTF
  IF lv_content2 IS NOT INITIAL.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer     = lv_content2
      TABLES
        binary_tab = lt_data2.
  ENDIF.

* Carregar HTML ABAPLIST
  IF lv_content IS NOT INITIAL.
    lo_html->load_data(
      EXPORTING
        type                 = 'application'
        subtype              = 'PDF'
      IMPORTING
        assigned_url         = lv_url
      CHANGING
        data_table           = lt_data
      EXCEPTIONS
        dp_invalid_parameter = 1
        dp_error_general     = 2
        cntl_error           = 3
        OTHERS               = 4 ).
  ENDIF.

* Carregar HTML OTF
  IF lv_content2 IS NOT INITIAL.
    lo_html2->load_data(
      EXPORTING
        type                 = 'application'
        subtype              = 'PDF'
      IMPORTING
        assigned_url         = lv_url2
      CHANGING
        data_table           = lt_data2
      EXCEPTIONS
        dp_invalid_parameter = 1
        dp_error_general     = 2
        cntl_error           = 3
        OTHERS               = 4 ).
  ENDIF.

* Exibir HTML ABAPLIST
  IF lv_content IS NOT INITIAL.
    CALL METHOD lo_html->show_url
      EXPORTING
        url      = lv_url
        in_place = 'X'.
  ENDIF.

* Exibir HTML OTF
  IF lv_content2 IS NOT INITIAL.
    CALL METHOD lo_html2->show_url
      EXPORTING
        url      = lv_url2
        in_place = 'X'.
  ENDIF.