Quick Guide: Create PDF with SIFbA

  • Dienstag, 01. März 2016 15:40
Artikel bewerten
(0 Stimmen)

Neue Klasse für das Erstellen von PDF mit Hilfe von SIFbA

Ich habe nun des öfteren SAP Interactive Forms generiert. Dabei bin ich immer wieder auf das Problem gestoßen, das ich irgendwas im Code vergessen haben. Anfangs habe ich mir ein Report mit den standardisierten Prozessen geschrieben. Irgendwie war das aber nicht dynamisch genug. Aus diesem Grund habe ich mir eine Klasse geschrieben welche eine statische Methode besitzt. Die Parameter dieser Methode sollten selbsterklärend sein. Sollten dennoch Rückfragen enstehen, so würde ich euch bitten, dieses via der Kommentarfunktion zu nutzen. :-)

Vorteil:

Nun kann ich mit einem einfachen Methodenaufruf "create_pdf" mir mein PDF generieren lassen und muss mir vorerst keine weiteren Gedanken über die Generierung machen. Außerdem kann nun aus jedem Report mit Hilfe dieser Klasse ein PDF generiert werden.

STATIC METHOD: CREATE_PDF

 *== create the pdf file
  zcl_zq01_create_pdf=>create_pdf(
      EXPORTING
        iv_formname    = 'ZQM60014_FORM'    " Name eines Formularobjektes
        iv_langu       = 'D'    " Sprachenschlüssel
        iv_country     = 'DE'    " Länderschlüssel
        iv_import_data = ls_export_pdf    " Struktur für data import --> Definition in der Schnittstelle des Forms
        iv_preview     = abap_true "|abap_false = PDF Vorschau?
        iv_filename    = 'your filename'
        iv_only_print  = abap_false "|abap_true
        iv_printer     = your tddest
      IMPORTING
        ev_filename    = data(lv_filename)
        es_formoutput  = data(ls_formoutput)
        EXCEPTIONS
        error_get_function_module_name    = 1
        error_open_print_job              = 2
        error_create_pdf_spooljob         = 3
        error_call_gen_function_module    = 4
        error_close_print_job             = 5
        OTHERS                            = 6
    ).
  IF sy-subrc <> 0.
*== no pdf is generated!!!
    MESSAGE e151(zq) DISPLAY LIKE 'E'.
    EXIT.
  ENDIF.

CLASS: ZCL_PDF_HANDLER

 CLASS zcl_pdf_handler DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    CLASS-METHODS create_pdf
      IMPORTING
        !iv_formname    TYPE fpname
        !iv_langu       TYPE langu
        !iv_country     TYPE land1
        !iv_import_data TYPE any     "zqm60014_sifba
        !iv_preview     TYPE char1
        !iv_filename    TYPE string
        !iv_only_print  TYPE abap_bool DEFAULT abap_false
        !iv_printer     TYPE sypdest
      EXPORTING
        !ev_filename    TYPE string
        !es_formoutput  TYPE fpformoutput
      EXCEPTIONS
        error_get_function_module_name
        error_open_print_job
        error_create_pdf_spooljob
        error_call_gen_function_module
        error_close_print_job .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS zcl_pdf_handler IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_PDF_HANDLER=>CREATE_PDF
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_FORMNAME                    TYPE        FPNAME
* | [--->] IV_LANGU                       TYPE        LANGU
* | [--->] IV_COUNTRY                     TYPE        LAND1
* | [--->] IV_IMPORT_DATA                 TYPE        ANY
* | [--->] IV_PREVIEW                     TYPE        CHAR1
* | [--->] IV_FILENAME                    TYPE        STRING
* | [--->] IV_ONLY_PRINT                  TYPE        ABAP_BOOL (default =ABAP_FALSE)
* | [--->] IV_PRINTER                     TYPE        SYPDEST
* | [<---] EV_FILENAME                    TYPE        STRING
* | [<---] ES_FORMOUTPUT                  TYPE        FPFORMOUTPUT
* | [EXC!] ERROR_GET_FUNCTION_MODULE_NAME
* | [EXC!] ERROR_OPEN_PRINT_JOB
* | [EXC!] ERROR_CREATE_PDF_SPOOLJOB
* | [EXC!] ERROR_CALL_GEN_FUNCTION_MODULE
* | [EXC!] ERROR_CLOSE_PRINT_JOB
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD create_pdf.
*** variables of get printers
    DATA: lv_short_printer TYPE sypdest,
          lv_spoolid       TYPE rspoid,
*** variables of the SIFBA specific calls
          ls_formoutput    TYPE fpformoutput,
          ls_outputparams  TYPE sfpoutputparams,
          lv_fm_name       TYPE rs38l_fnam,
          ls_docparams     TYPE sfpdocparams,
          lt_binary_tab    TYPE solix_tab,
          lv_lifetime      TYPE sypexpi,
          lv_filepath      TYPE string,
          lv_filename      TYPE string,
          lv_output_length TYPE i,
          lv_error_string  TYPE string.
*== First get name of the generated function module
    TRY.
        CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
          EXPORTING
            i_name     = iv_formname
          IMPORTING
            e_funcname = lv_fm_name.
      CATCH cx_root.
        RAISE error_get_function_module_name.
        MESSAGE e001(fp_example).
    ENDTRY.
*== Open the spool job
    ls_outputparams-nodialog = 'X'.    " suppress printer dialog popup
*-- pdf preview?
    CASE iv_preview.
      WHEN 'X'.
        ls_outputparams-preview  = 'X'.    " launch print preview
*        ls_outputparams-getpdf   = 'X'.    " get pdf file
      WHEN ' '.
*        ls_outputparams-preview  = 'X'.    " launch print preview
        ls_outputparams-getpdf   = 'X'.    " get pdf file
    ENDCASE.
*-- get the correct printer destination @pdf printer type
*-- set Spool: Ausgabegerät
    IF iv_only_print EQ abap_true.
      CALL FUNCTION 'CONVERSION_EXIT_SPDEV_INPUT'
        EXPORTING
          input  = iv_printer &amp;& 'PDF'
        IMPORTING
          output = lv_short_printer.
      ls_outputparams-dest = lv_short_printer.
    ELSE.
      ls_outputparams-dest     = 'X1G2'. " printer??
    ENDIF.
    ls_outputparams-reqnew = abap_true."Neuer Spool-Auftrag
    ls_outputparams-reqimm = abap_true."Spool-Sofortdruck

    CALL FUNCTION 'FP_JOB_OPEN'
      CHANGING
        ie_outputparams = ls_outputparams
      EXCEPTIONS
        cancel          = 1
        usage_error     = 2
        system_error    = 3
        internal_error  = 4
        OTHERS          = 5.

    IF sy-subrc <> 0.
      RAISE error_open_print_job.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      EXIT.
    ENDIF.
*== Configure doc parameters ------------------------*
*-- set form language and country (->form locale)
    ls_docparams-langu   = iv_langu.
    ls_docparams-country = iv_country.
*-- Set interactive & dynamic flag
*    ls_docparams-fillable = 'abap_true'. --> interactive formular?
    ls_docparams-dynamic  = 'abap_true'.
*== Call the generated function module for form generation
    CALL FUNCTION lv_fm_name
      EXPORTING
        /1bcdwb/docparams  = ls_docparams
*-- only the needed parameters for this fm
        is_import_data     = iv_import_data
      IMPORTING
        /1bcdwb/formoutput = ls_formoutput
      EXCEPTIONS
        usage_error        = 1
        system_error       = 2
        internal_error     = 3
        graphic_not_found  = 4
        OTHERS             = 5.

    IF sy-subrc <> 0.
*== Problems -->
* a) Standardtexte vorhanden? --> Transaktion: SO10
* b) Sachverhalt Text vorhanden?
* c) important data @ iv_import_data initial?
      CALL FUNCTION 'FP_GET_LAST_ADS_ERRSTR'
        IMPORTING
          e_adserrstr = lv_error_string.
      RAISE error_call_gen_function_module.
      IF NOT lv_error_string IS INITIAL.
*     we received a detailed error description
        WRITE:/ lv_error_string.
        EXIT.
      ELSE.
        WRITE: / 'FP_GET_LAST_ADS_ERRSTR send back empty error string!'.
      ENDIF.
    ELSE.
      WRITE: / 'Everything okay, the form has been created!'.
      MESSAGE 'pdf created successfully' TYPE 'S'.
    ENDIF.

*== instant print?
    IF iv_only_print EQ abap_true.
*-- generate a spool job and print immediate
*   Dieser Baustein ADS CREATE PDF SPOOLJOB kann ein PDF im Format XSTRING direkt in einen Spool-Auftrag schreiben.
*   Dieser FuBa muss verwendet werden, wenn der Parameter ***-getpdf   = abap_true gesetzt wurde.
*   Der Drucker beim Parameter DEST muss den Gerätetyp PDF haben. Sonst gibt es eine Fehlermeldung.
      lv_lifetime = ls_outputparams-lifetime.
      CALL FUNCTION 'ADS_CREATE_PDF_SPOOLJOB'
        EXPORTING
          dest              = lv_short_printer
          pages             = ls_formoutput-pages
          pdf_data          = ls_formoutput-pdf
          name              = ls_outputparams-dataset
          suffix1           = iv_printer
          suffix2           = ls_outputparams-suffix2
          immediate_print   = abap_true
          auto_delete       = abap_true
          lifetime          = lv_lifetime
        IMPORTING
          spoolid           = lv_spoolid
        EXCEPTIONS
          no_data           = 1
          not_pdf           = 2
          wrong_devtype     = 3
          operation_failed  = 4
          cannot_write_file = 5
          device_missing    = 6
          no_such_device    = 7
          OTHERS            = 8.
      IF sy-subrc <> 0.
        RAISE error_create_pdf_spooljob.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ELSE.
        MESSAGE s060(ssfcomposer) WITH lv_spoolid.
      ENDIF.
    ENDIF.
*== Close the spool job
    CALL FUNCTION 'FP_JOB_CLOSE'
      EXCEPTIONS
        usage_error    = 1
        system_error   = 2
        internal_error = 3
        OTHERS         = 4.
    IF sy-subrc <> 0.
      RAISE error_close_print_job.
      WRITE: / 'Problems during the FP_JOB_CLOSE, sy-subrc = ',
               sy-subrc COLOR COL_NEGATIVE.
    ENDIF.
*== get user sap work dir
    IF iv_preview IS INITIAL.
      MESSAGE 'get_sapgui_workdir' TYPE 'S'.
*      IF iv_only_print EQ abap_false.
      IF sy-oncom NE 'V'.
        CALL METHOD cl_gui_frontend_services=>get_sapgui_workdir
          CHANGING
            sapworkdir = lv_filepath.
*== set pdf filename including *** & .pdf
        lv_filename = lv_filepath && '\'
                      && iv_filename
*                    && '_' && sy-datum
                      && '.pdf'.
      ELSE.
        CONCATENATE '/sapmnt/' sy-sysid '/global/temp/' iv_filename '.pdf' INTO lv_filename.
      ENDIF.
*== convert the xtring from form to binary
      CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          buffer        = ls_formoutput-pdf
        IMPORTING
          output_length = lv_output_length
        TABLES
          binary_tab    = lt_binary_tab.

*== create the pdf file @ local sap workdir | SAP Backend Server Folder
      MESSAGE 'gui_download' TYPE 'S'.
*      IF iv_only_print EQ abap_false.
      IF sy-oncom NE 'V'.
        CALL METHOD cl_gui_frontend_services=>gui_download
          EXPORTING
            bin_filesize = lv_output_length    " Dateilänge bei Binärdateien
            filename     = lv_filename    " Name der Datei
            filetype     = 'BIN'    " Dateityp (Ascii, Binär, ...)
          CHANGING
            data_tab     = lt_binary_tab    " Übergabetabelle
          EXCEPTIONS
            OTHERS       = 1.
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.
      ELSE.
        FIELD-SYMBOLS: <fs_binary_tab> TYPE any.
        OPEN DATASET lv_filename FOR OUTPUT IN BINARY MODE.
        LOOP AT lt_binary_tab ASSIGNING <fs_binary_tab>.
          IF <fs_binary_tab> IS ASSIGNED.
            TRANSFER <fs_binary_tab> TO lv_filename.
          ENDIF.
        ENDLOOP.
        CLOSE DATASET lv_filename.
      ENDIF.
      ev_filename = iv_filename && '.pdf'.
      es_formoutput = ls_formoutput.
    ENDIF.

  ENDMETHOD.
ENDCLASS.

Was haltet ihr davon? Könnte man was in dieser Klasse optimieren?

Letzte Änderung am Mittwoch, 08 Juni 2016 12:23
Kommentare einblenden

Copyright © 2017 ABAP-Workbench.de