Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Save PDF File Without Display

Hi Folks,

I have a scenario where i need to save a PDF file on desktop without opening it. I've implemented the following approach and code:

1. Created a UI Element fileDownload and associated it with PDFSource variable of type XSTRING in context

2. Called the following method in init method of view


METHOD manual_save .

* Table in XML Format
  DATA : ixml TYPE string.

* Table Data in Hexadecimal Format
  DATA : lv_xstring TYPE xstring.

* Function Module Calling
  DATA : w_fmname TYPE rs38l_fnam,     "FM Name
         fp_outputparams   TYPE sfpoutputparams. "FM Output Parameters

* Form Parameters
  DATA :   fpdocparams TYPE sfpdocparams ,   "Form Parameters
           fpformoutput TYPE fpformoutput .  "Form Output Data

* Get Context Refrence and Table Data
  DATA lo_nd_skutab TYPE REF TO if_wd_context_node.
  DATA lo_nd_skudata TYPE REF TO if_wd_context_node.
  DATA lo_el_skudata TYPE REF TO if_wd_context_element.
  DATA ls_skudata TYPE wd_this->element_skudata.
* navigate from <CONTEXT> to <SKUTAB> via lead selection
  lo_nd_skutab = wd_context->get_child_node( name = wd_this->wdctx_skutab ).

* navigate from <SKUTAB> to <SKUDATA> via lead selection
  lo_nd_skudata = lo_nd_skutab->get_child_node( name = wd_this->wdctx_skudata ).

* @TODO handle not set lead selection
  IF lo_nd_skudata IS INITIAL.
  ENDIF.

* get element via lead selection
  lo_el_skudata = lo_nd_skudata->get_element(  ).

* @TODO handle not set lead selection
  IF lo_el_skudata IS INITIAL.
  ENDIF.

* get all declared attributes
  lo_el_skudata->get_static_attributes(
    IMPORTING
      static_attributes = ls_skudata ).

* Convert Table Data to XML Format (type String)
  CALL METHOD lo_nd_skudata->to_xml
    RECEIVING
      xml = ixml.

* Convert String to Xstring Format

*CALL FUNCTION 'CRM_IC_XML_STRING2XSTRING'
*  EXPORTING
*    instring         = IXML
* IMPORTING
*   OUTXSTRING       = LV_XSTRING
  .

  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text           = ixml
*   MIMETYPE       = ' '
*   ENCODING       =
    IMPORTING
      buffer         = lv_xstring
 EXCEPTIONS
   failed         = 1
   OTHERS         = 2
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


*  Set Output Parameters
  fp_outputparams-nodialog = 'X'.
  fp_outputparams-getpdf = 'X'.
  fp_outputparams-connection = 'ADS'.


*  Job Open
  CALL FUNCTION 'FP_JOB_OPEN'
    CHANGING
      ie_outputparams = fp_outputparams
    EXCEPTIONS
      cancel          = 1
      usage_error     = 2
      system_error    = 3
      internal_error  = 4
      OTHERS          = 5.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

*&-  Identify FM for PDF Form
  CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
    EXPORTING
      i_name           = 'ZTEST_SKU_PDF_FORM'
    IMPORTING
      e_funcname       = w_fmname
*      e_interface_type =
      .

* set form parametsrs
  fpdocparams-langu = 'E'.
  fpdocparams-country = 'US'.
*fpdocparams-FILLABLE = 'X'.

* Generate form
  CALL FUNCTION w_fmname      "'/1BCDWB/SM00000021'
    EXPORTING
     /1bcdwb/docparams        = fpdocparams
      /1bcdwb/docxml           = lv_xstring
   IMPORTING
     /1bcdwb/formoutput       = fpformoutput
   EXCEPTIONS
     usage_error              = 1
     system_error             = 2
     internal_error           = 3
     OTHERS                   = 4
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

* Job Close
  CALL FUNCTION 'FP_JOB_CLOSE'
    EXCEPTIONS
      usage_error    = 1
      system_error   = 2
      internal_error = 3
      OTHERS         = 4.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Bind Data to PDF source
  DATA lo_el_context TYPE REF TO if_wd_context_element.
  DATA ls_context TYPE wd_this->element_context.
  DATA lv_pdf LIKE ls_context-pdf.
* get element via lead selection
  lo_el_context = wd_context->get_element(  ).

  lv_pdf = fpformoutput-pdf.

  CALL METHOD lo_el_context->set_attribute
    EXPORTING
      value = lv_pdf
      name  = 'PDF'.


ENDMETHOD.

To explain in simple terms, i've done the following:

1. Stored the context table data in a XML format in a string variable

2. Converted the string variable to xstring format using fm 'SCMS_STRING_TO_XSTRING'

3. Job Open

4. Identify the Actual FM Name

5. Genarate Form

6. Job Close

7. Bind data (fpformoutput-pdf) to the PDF Source variable in context

Issue is, when i run application and download, i am only able to get a PDf with no data in it.

Interestingly, in debug mode, i am able to see some data.

Points to add: When i display in a PDF UIElement the output comes in two pages, and in debug mode when i see the variable FPFORMOUTPUT-PAGES, it has a value of 1.

May be am following the wrong approach or missing someting in code.

Any inputs, appreciated.

Thanks,

Santosh Verma,

Deloitte.

Former Member
Former Member replied

I find the problem, why your pdf do not get the data. Because when you get the ixml from to_XML method, there will be a redundant pair of tabs, <context>...</context> in the string. It is the why the pdf can not parse the string, because these tabs break the mapping relationship in pdf. Eliminate them and try again.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question