Smartform表单转PDF文件流 http上传

  DATA: lv_fm_name TYPE rs38l_fnam.
  DATA: l_cx_root TYPE REF TO cx_root.
  DATA: lv_logo_sa TYPE string.
  DATA: ls_formoutput TYPE ssfcrescl.
  DATA: l_fncname TYPE rs38l_fnam, "smartform 动态函数名
         lw_control_param TYPE ssfctrlop, "Smart Forms: 控制结构
         lw_job_output_info TYPE ssfcrescl, "Smart Forms: 表格打印结束时返回值
         lt_itcoo TYPE itcoo OCCURS 0, "Smart Forms: OTF 结构
         lw_composer_param TYPE ssfcompop .
  DATA: lv_inst_id TYPE zflow_inst_id.
  DATA: li_http_client TYPE REF TO if_http_client.
  DATA: part TYPE REF TO if_http_entity.


  TYPES:BEGIN OF ty_body,
         file TYPE xstring,
        END OF ty_body.
  DATA:lw_body TYPE ty_body,
       lw_rev TYPE  zsfi261,
       lv_json TYPE string,
       lv_value TYPE string,
       l_rest    TYPE REF TO zcl_fi_rest,
       lv_res TYPE string,
       lv_url       TYPE string,
       lv_rev    TYPE string.


  TRY .
	CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
		EXPORTING
            formname = 'ZFIF007'       "SMART formS的名字
		IMPORTING
            fm_name  = lv_fm_name.
	CATCH cx_root INTO l_cx_root.
        ev_msg = l_cx_root->get_text( ).
        ev_flag = 'E'.
	CONTINUE.
  ENDTRY.


    lw_control_param-langu = '1'.
    lw_control_param-no_open = 'X'.
    lw_control_param-no_close = 'X'.
    lw_control_param-preview = 'X'.
    lw_control_param-no_dialog = 'X'.
    lw_control_param-getotf = 'X'.

*   lw_composer_param-tdimmed = 'X'.
*   lw_composer_param-tdcopies = '1'.
*   lw_composer_param-tddelete = 'X'.
*   lw_composer_param-tdnoprint = ''.
    lw_composer_param-tddest = 'LP01'.
    lw_composer_param-tdprinter = 'CNSAPWIN'.
  CALL FUNCTION 'SSF_OPEN'
	EXPORTING
        user_settings      = ''
        control_parameters = lw_control_param
        output_options     = lw_composer_param
*     IMPORTING
*       job_output_options = lw_outopt
	EXCEPTIONS
        formatting_error   = 1
        internal_error     = 2
        send_error         = 3
        user_canceled      = 4
		OTHERS             = 5.

	IF sy-subrc <> 0.
      ev_flag = 'E'.
	CONTINUE.
  ENDIF.

  CALL FUNCTION lv_fm_name
	EXPORTING
*       /1BCDWB/DOCPARAMS  =
        control_parameters = lw_control_param
        output_options     = lw_composer_param
	IMPORTING
        job_output_info    = ls_formoutput
	TABLES
        t_head             = lt_head
        t_item             = lt_item
	EXCEPTIONS
        usage_error        = 1
        system_error       = 2
        internal_error     = 3
		OTHERS             = 4.
  IF sy-subrc <> 0.
      ev_flag = 'E'.	
  ENDIF.

  CALL FUNCTION 'SSF_CLOSE'
	IMPORTING
        job_output_info  = ls_formoutput
	EXCEPTIONS
        formatting_error = 1
        internal_error   = 2
        send_error       = 3
		OTHERS           = 4.
  IF sy-subrc <> 0.
      ev_flag = 'E'.
	CONTINUE.
  ENDIF.
  APPEND LINES OF ls_formoutput-otfdata TO lt_itcoo[].


"将OTF转换PDF格式
DATA: lt_lines TYPE STANDARD TABLE OF tline, "SAPscript: 文字行
          lw_lines TYPE tline,
          lt_docs TYPE  TABLE OF docs, "存储文档
          l_uuid      TYPE sysuuid_c,
          l_bin_size TYPE i. "
FIELD-SYMBOLS <fs_line> TYPE x.
DATA: lv_pdf TYPE xstring.

CALL FUNCTION 'CONVERT_OTF_2_PDF'
* EXPORTING
*   USE_OTF_MC_CMD               = 'X'
*   ARCHIVE_INDEX                =
IMPORTING
        bin_filesize   = l_bin_size
TABLES
        otf            = lt_itcoo
        doctab_archive = lt_docs
lines          = lt_lines
* EXCEPTIONS
*       ERR_CONV_NOT_POSSIBLE        = 1
*       ERR_OTF_MC_NOENDMARKER       = 2
*       OTHERS         = 3
.
IF sy-subrc <> 0.
      ev_flag = 'E'.
CONTINUE.
ENDIF.

LOOP AT lt_lines INTO lw_lines.
ASSIGN lw_lines TO <fs_line> CASTING.
CONCATENATE lv_pdf <fs_line> INTO lv_pdf IN BYTE MODE.
ENDLOOP.

*    lw_body-file = lv_pdf.
   
CALL METHOD cl_http_client=>create_by_url
EXPORTING
        url                = lv_url
IMPORTING
client             = li_http_client
EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
OTHERS             = 4.
IF sy-subrc <> 0.
MESSAGE text-010 TYPE 'E'.
RETURN.
ENDIF.
"设置参数
    li_http_client->request->set_method( if_http_request=>co_request_method_post ).
CALL METHOD li_http_client->request->set_content_type
EXPORTING
        content_type = 'multipart/form-data'.
CALL METHOD li_http_client->request->set_header_field
EXPORTING
        name  = 'x-timevale-project-id'
value = ''.
CALL METHOD li_http_client->request->set_header_field
EXPORTING
        name  = 'x-timevale-signature'
value = ''.
CALL METHOD li_http_client->request->if_http_entity~set_header_field
EXPORTING
        name  = 'Content-Type'
value = 'multipart/form-data;charset=utf-8'.
"设置body
    part = li_http_client->request->if_http_entity~add_multipart( ).
CALL METHOD part->set_header_field
EXPORTING
        name  = 'content-type'
value = 'application/pdf'.
    lv_value = 'form-data; name="file";filename="file' && <ls_head>-zdxqdbh && '.pdf'.
    part->set_header_field( name  = 'Content-Disposition' value = lv_value ).

	CALL METHOD part->set_data
		EXPORTING
			data = lv_pdf.
    li_http_client->send(  EXCEPTIONS http_communication_failure = 1
      http_invalid_state         = 2 ).
	IF sy-subrc NE 0.
      li_http_client->get_last_error( IMPORTING message = ev_msg ).
	ELSE.
      li_http_client->receive( EXCEPTIONS http_communication_failure = 1
                                           http_invalid_state         = 2
                                           http_processing_failed     = 3 ).
	  IF sy-subrc <> 0 .
        li_http_client->get_last_error( IMPORTING message = ev_msg ).
	  ELSE.
        lv_res = li_http_client->response->get_cdata( ).
        li_http_client->close( ).
	  ENDIF.
ENDIF.