这是项目上看到的绝技^_^
1.画SF。这步就不说了
2.确定参数,写SF打印PDF函数
FUNCTION zsrv_print_to_pdf .
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IV_SAMRTFORM) TYPE TDSFNAME
*" REFERENCE(IV_GUID) TYPE CRMT_OBJECT_GUID OPTIONAL
*" REFERENCE(IV_OBJECT_ID) TYPE CRMT_OBJECT_ID OPTIONAL
*" REFERENCE(IV_ZSERIALNO) TYPE ZSERIALNO OPTIONAL
*" EXPORTING
*" REFERENCE(FILE) TYPE XSTRING
*"----------------------------------------------------------------------
DATA: lv_fmodule TYPE rs38l_fnam,
lv_formfm TYPE rs38l_fnam,
lt_data_exchange TYPE TABLE OF zreq_chg,
lt_data_refund TYPE TABLE OF zreq_chg,
lt_data_dispatch TYPE TABLE OF zcrm_csr_order,
lt_data_service TYPE TABLE OF zsv_ord,
lt_text TYPE comt_text_lines_t,
lv_tablename TYPE string,
lv_exit_check TYPE flag,
lv_reason TYPE string.
DATA:
lv_langu TYPE sy-langu,
w_cparam TYPE ssfctrlop,
w_outoptions TYPE ssfcompop,
w_bin_filesize TYPE i, " Binary File Size
w_bin_file TYPE xstring.
DATA:GV_ZNUMBER TYPE ZNUMBER.
DATA:GW_ZHSI_REFURBISH TYPE ZHSI_REFURBISH.
DATA: t_otf TYPE TABLE OF itcoo,
* Internal table to hold OTF data recd from the SMARTFORM
t_otf_from_fm TYPE ssfcrescl,
* Internal table to hold the data from the FM CONVERT_OTF
t_pdf_tab TYPE TABLE OF tline.. FIELD-SYMBOLS <fs_data> TYPE table. IF iv_samrtform IS INITIAL.
RETURN.
ENDIF. CASE iv_samrtform.
WHEN 'ZEXCH'.
lv_fmodule = 'ZGET_EXCHANGE_REQUEST_DATA'.
WHEN 'ZREFUND'.
lv_fmodule = 'ZGET_REFUND_REQUEST_DATA'.
WHEN 'Z_DISPATCH'.
lv_fmodule = 'ZGET_CSR_DISPATCH_ORDER_DATA'.
WHEN 'ZSERVICE_ORD' OR 'ZSERVICE_ORD_AU' OR 'ZSERVICE_ORD_ZA'.
lv_fmodule = 'ZGET_SERVICE_ORDER_DATA'.
WHEN 'ZSRV_ORD' OR 'ZSRV_ORD_N' OR 'ZSRV_ORD_ZA'.
lv_fmodule = 'ZGET_SERVICE_ORDER_DATA'.
WHEN 'ZREPAIR_REP01'.
lv_exit_check = 'X'.
WHEN 'ZCRM_SERVICE_01'.
lv_exit_check = 'X'.
WHEN 'ZCRM_SERVICE_02'.
lv_exit_check = 'X'.
ENDCASE. IF lv_exit_check IS INITIAL.
CALL FUNCTION 'FUNCTION_EXISTS'
EXPORTING
funcname = lv_fmodule
EXCEPTIONS
function_not_exist =
OTHERS = .
IF sy-subrc <> .
RETURN.
ENDIF.
ENDIF. CASE iv_samrtform.
WHEN 'ZEXCH'.
CALL FUNCTION lv_fmodule
EXPORTING
iv_guid = iv_guid
iv_object_id = iv_object_id
IMPORTING
reasons = lv_reason
TABLES
data = lt_data_exchange. WHEN 'ZREFUND'.
CALL FUNCTION lv_fmodule
EXPORTING
iv_guid = iv_guid
iv_object_id = iv_object_id
IMPORTING
reasons = lv_reason
TABLES
data = lt_data_refund. WHEN 'Z_DISPATCH'.
CALL FUNCTION lv_fmodule
EXPORTING
iv_guid = iv_guid
iv_object_id = iv_object_id
TABLES
data = lt_data_dispatch. WHEN 'ZSERVICE_ORD' OR 'ZSERVICE_ORD_AU' OR 'ZSERVICE_ORD_ZA'.
CALL FUNCTION lv_fmodule
EXPORTING
iv_guid = iv_guid
iv_object_id = iv_object_id
TABLES
data = lt_data_service. WHEN 'ZSRV_ORD' OR 'ZSRV_ORD_N' OR 'ZSRV_ORD_ZA'.
CALL FUNCTION lv_fmodule
EXPORTING
iv_guid = iv_guid
iv_object_id = iv_object_id
TABLES
data = lt_data_service. ENDCASE. CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = iv_samrtform
IMPORTING
fm_name = lv_formfm
EXCEPTIONS
no_form =
no_function_module =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. * Calling the SMARTFORM using the function module retrieved above
* GET_OTF parameter in the CONTROL_PARAMETERS is set to get the OTF
* format of the output
w_cparam-no_dialog = 'X'.
w_cparam-preview = space. " Suppressing the dialog box w_cparam-getotf = 'X'. " for print preview ****---add by alex_zhang on 20160901
* w_cparam-no_open = 'X'.
* w_cparam-no_close = 'X'. * Printer name to be used is provided in the export parameter
* OUTPUT_OPTIONS
w_outoptions-tddest = 'LP01'. **********************************************************************20161117-Begin
*开发顾问:陈国光
*业务顾问:龚慧文
*打印中文乱码该方案仅针对UI按钮打印 未能从根本解决问题 故先取消
* IF zcl_crm_attributes=>gv_zcrm_print = abap_true.
* w_cparam-langu = '1'.
* zcl_crm_attributes=>gv_zcrm_print = abap_false.
* ENDIF.
**********************************************************************20161117-End
CASE iv_samrtform.
WHEN 'ZEXCH'.
CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
reasons = lv_reason
IMPORTING
job_output_info = t_otf_from_fm
TABLES
datasets = lt_data_exchange
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. WHEN 'ZREFUND'.
CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
reasons = lv_reason
IMPORTING
job_output_info = t_otf_from_fm
TABLES
datasets = lt_data_refund
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. WHEN 'Z_DISPATCH'.
CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
IMPORTING
job_output_info = t_otf_from_fm
TABLES
zorder = lt_data_dispatch
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. WHEN 'ZSERVICE_ORD' OR 'ZSERVICE_ORD_AU' OR 'ZSERVICE_ORD_ZA'.
CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
IMPORTING
job_output_info = t_otf_from_fm
TABLES
datasets = lt_data_service
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. WHEN 'ZSRV_ORD' OR 'ZSRV_ORD_N' OR 'ZSRV_ORD_ZA'.
CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
IMPORTING
job_output_info = t_otf_from_fm
TABLES
datasets = lt_data_service
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. WHEN 'ZREPAIR_REP01'.
CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
object_id = iv_object_id
IMPORTING
job_output_info = t_otf_from_fm
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WHEN 'ZCRM_SERVICE_01'."ADD BY LY 20170217 FOR 维修跟踪单打印
CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
object_id = iv_object_id
IMPORTING
job_output_info = t_otf_from_fm
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. WHEN 'ZCRM_SERVICE_02'."ADD BY LY 20170217 FOR 周转机检查单打印
"生成不良品再生单单号
PERFORM prm_generate_obj CHANGING GV_ZNUMBER. CALL FUNCTION lv_formfm
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
ZSERIALNO = iv_ZSERIALNO
ZNUMBER = GV_ZNUMBER
IMPORTING
job_output_info = t_otf_from_fm
EXCEPTIONS
formatting_error =
internal_error =
send_error =
user_canceled =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. SELECT SINGLE ZSERIALNO
ZPRODUCT_ID
ZCMODENO
ZSTORE_LOC
INTO (GW_ZHSI_REFURBISH-ZCSERIALNO,GW_ZHSI_REFURBISH-ZMODENO,
GW_ZHSI_REFURBISH-ZCMODENO,GW_ZHSI_REFURBISH-ZLGORT)
FROM ZSRVT_JA_001 WHERE ZSERIALNO = iv_ZSERIALNO.
GW_ZHSI_REFURBISH-ZNUMBER = GV_ZNUMBER.
GW_ZHSI_REFURBISH-ZBATCHNO = 'H'.
GW_ZHSI_REFURBISH-ZRFSTATUS = ''.
GW_ZHSI_REFURBISH-ZCOUNTRY = 'JP'.
GW_ZHSI_REFURBISH-ZSALES_ORG = 'O 50000231'.
GW_ZHSI_REFURBISH-ZWERKS = ''.
GW_ZHSI_REFURBISH-CREATED_BY = SY-UNAME.
CONVERT DATE SY-DATUM TIME SY-UZEIT INTO TIME STAMP
GW_ZHSI_REFURBISH-CREATED_AT TIME ZONE SY-ZONLO. MODIFY ZHSI_REFURBISH FROM GW_ZHSI_REFURBISH. ENDCASE. t_otf[] = t_otf_from_fm-otfdata[]. CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
max_linewidth =
IMPORTING
bin_filesize = w_bin_filesize
bin_file = w_bin_file
TABLES
otf = t_otf
lines = t_pdf_tab
EXCEPTIONS
err_max_linewidth =
err_format =
err_conv_not_possible =
err_bad_otf =
OTHERS = .
IF sy-subrc <> .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
MOVE w_bin_file TO file.
ENDFUNCTION.
3.将输出的PDF文件流用PDF展示:
创建ZCRM_PRINT WDA程序
Window中MODIFY window
METHOD wddomodifyview .
* variable declarations
DATA:
w_form_name TYPE tdsfname,
w_fmodule TYPE rs38l_fnam,
w_cparam TYPE ssfctrlop,
w_outoptions TYPE ssfcompop,
w_bin_filesize TYPE i, " Binary File Size
w_bin_file TYPE xstring,
w_filename TYPE string,
w_file_path TYPE string,
w_full_path TYPE string. DATA lo_nd_order TYPE REF TO if_wd_context_node.
DATA lo_el_order TYPE REF TO if_wd_context_element.
DATA ls_order TYPE wd_this->element_order.
DATA lo_nd_pdf TYPE REF TO if_wd_context_node.
DATA lo_el_pdf TYPE REF TO if_wd_context_element.
DATA ls_pdf TYPE wd_this->element_pdf.
DATA lv_source TYPE wd_this->element_pdf-source. lo_nd_order = wd_context->get_child_node( name = wd_this->wdctx_order ).
lo_el_order = lo_nd_order->get_element( ).
lo_el_order->get_static_attributes( IMPORTING static_attributes = ls_order ). zcl_crm_attributes=>gv_zcrm_print = abap_true. CALL FUNCTION 'ZSRV_PRINT_TO_PDF'
EXPORTING
iv_samrtform = ls_order-smartform_name "'ZREFUND'
iv_object_id = ls_order-object_id "'0074000104'
IV_ZSERIALNO = ls_order-ZSERIALNO "'0074000104'
IMPORTING
file = w_bin_file. CHECK w_bin_file IS NOT INITIAL. lo_nd_pdf = wd_context->get_child_node( name = wd_this->wdctx_pdf ).
lo_el_pdf = lo_nd_pdf->get_element( ).
lv_source = w_bin_file.
lo_el_pdf->set_attribute( name = `SOURCE` value = lv_source ).
ENDMETHOD.
4.window传入参数:
METHOD handledefault .
DATA lo_nd_order TYPE REF TO if_wd_context_node. DATA lo_el_order TYPE REF TO if_wd_context_element.
DATA ls_order TYPE wd_this->element_order. lo_nd_order = wd_context->get_child_node( name = wd_this->wdctx_order ).
lo_el_order = lo_nd_order->get_element( ).
lo_el_order->get_static_attributes( IMPORTING static_attributes = ls_order ). ls_order-object_id = object_id.
ls_order-smartform_name = smartform_name.
ls_order-ZSERIALNO = ZSERIALNO."ADD BY LY 20170220 lo_el_order->set_static_attributes( static_attributes = ls_order ).
ENDMETHOD.
5.最后将WINDOW做成接口视图
6.UI调用
GET_BUTTONS/DO_PREPARE_OUTPUT
METHOD IF_BSP_WD_TOOLBAR_CALLBACK~GET_BUTTONS.
DATA: LS_BUTTON TYPE CRMT_THTMLB_BUTTON_EXT.
DATA: LV_URL TYPE STRING,
LV_URL2 TYPE STRING,
LV_OBJECT_ID TYPE CRMT_OBJECT_ID,
LV_SRV_TYPE TYPE ZDTEL00003Y,
LV_SMARTFORM_NAME TYPE TDSFNAME. DATA: LR_ENTITY TYPE REF TO CL_CRM_BOL_ENTITY,
LR_COLL TYPE REF TO IF_BOL_ENTITY_COL,
LR_CURRENT TYPE REF TO IF_BOL_BO_PROPERTY_ACCESS,
LR_ENTITY_ORG TYPE REF TO CL_CRM_BOL_ENTITY,
LV_PROCESS_TYPE TYPE STRING,
LS_ORGSET_BTIL TYPE CRMST_ORGSET_BTIL. FIELD-SYMBOLS: <FS_OBJECT_ID> TYPE CRMT_OBJECT_ID. CALL METHOD SUPER->IF_BSP_WD_TOOLBAR_CALLBACK~GET_BUTTONS
RECEIVING
RT_BUTTONS = RT_BUTTONS. DELETE RT_BUTTONS WHERE ON_CLICK = 'OUTPUT'.
DELETE RT_BUTTONS WHERE ON_CLICK = 'PRINT_PREVIEW'. LR_ENTITY ?= ME->ZTYPED_CONTEXT->BTADMINH->COLLECTION_WRAPPER->GET_CURRENT( ).
IF LR_ENTITY IS BOUND.
TRY.
LV_PROCESS_TYPE = LR_ENTITY->GET_PROPERTY_AS_STRING( IV_ATTR_NAME = 'PROCESS_TYPE').
CALL METHOD LR_ENTITY->IF_BOL_BO_PROPERTY_ACCESS~GET_PROPERTY_AS_VALUE
EXPORTING
IV_ATTR_NAME = 'OBJECT_ID'
IMPORTING
EV_RESULT = LV_OBJECT_ID.
LR_COLL = LR_ENTITY->GET_RELATED_ENTITIES( IV_RELATION_NAME = 'BTHeaderOrgmanSet' ). "
LR_CURRENT = LR_COLL->GET_CURRENT( ).
IF LR_CURRENT IS BOUND.
LR_ENTITY_ORG ?= LR_CURRENT.
LR_ENTITY_ORG->IF_BOL_BO_PROPERTY_ACCESS~GET_PROPERTIES( IMPORTING ES_ATTRIBUTES = LS_ORGSET_BTIL ).
ENDIF.
CATCH CX_CRM_CIC_PARAMETER_ERROR.
ENDTRY.
ENDIF. CHECK LV_OBJECT_ID IS NOT INITIAL. CHECK LV_PROCESS_TYPE = 'ZSR2' OR LV_PROCESS_TYPE = 'ZSR3' OR LV_PROCESS_TYPE = 'ZSV1' OR LV_PROCESS_TYPE = 'ZSV2' OR LV_PROCESS_TYPE = 'ZSV3' OR LV_PROCESS_TYPE = 'ZWO4'. CASE LV_PROCESS_TYPE.
WHEN 'ZSR2'.
LV_SMARTFORM_NAME = 'ZEXCH'.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL. WHEN 'ZSR3'.
LV_SMARTFORM_NAME = 'ZREFUND'.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL. WHEN 'ZSV1'.
IF LS_ORGSET_BTIL-SALES_ORG = 'O 50000178'.
LV_SMARTFORM_NAME = 'ZSRV_ORD'.
ELSEIF LS_ORGSET_BTIL-SALES_ORG = 'O 50000192' OR LS_ORGSET_BTIL-SALES_ORG = 'O 50000194'.
LV_SMARTFORM_NAME = 'ZSRV_ORD_ZA'.
ELSE.
LV_SMARTFORM_NAME = 'ZSRV_ORD_N'.
ENDIF.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL. WHEN 'ZSV2'.
IF LS_ORGSET_BTIL-SALES_ORG = 'O 50000192' OR LS_ORGSET_BTIL-SALES_ORG = 'O 50000194'.
LV_SMARTFORM_NAME = 'ZSERVICE_ORD_ZA'.
ELSE.
LV_SMARTFORM_NAME = 'ZSERVICE_ORD'.
ENDIF.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL. WHEN 'ZSV3'.
IF LS_ORGSET_BTIL-SALES_ORG = 'O 50000131'.
LV_SMARTFORM_NAME = 'ZSERVICE_ORD_AU'.
ELSEIF LS_ORGSET_BTIL-SALES_ORG = 'O 50000192' OR LS_ORGSET_BTIL-SALES_ORG = 'O 50000194'.
LV_SMARTFORM_NAME = 'ZSERVICE_ORD_ZA'.
ELSE.
LV_SMARTFORM_NAME = 'ZSERVICE_ORD'.
ENDIF.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL. WHEN 'ZWO4'.
LV_SMARTFORM_NAME = 'ZSERVICE_ORD'.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL. WHEN OTHERS.
ENDCASE.
CLEAR LS_BUTTON.
LS_BUTTON-TEXT = CL_WD_UTILITIES=>GET_OTR_TEXT_BY_ALIAS( 'ZCRM/PDFPRINT' ). "#EC NOTEXT
LS_BUTTON-ON_CLIENT_CLICK = LV_URL.
LS_BUTTON-ENABLED = ABAP_TRUE. "#EC NOTEXT
INSERT LS_BUTTON INTO RT_BUTTONS INDEX . CALL METHOD LR_ENTITY->IF_BOL_BO_PROPERTY_ACCESS~GET_PROPERTY_AS_VALUE
EXPORTING
IV_ATTR_NAME = 'ZZFLD00000N'
IMPORTING
EV_RESULT = LV_SRV_TYPE. CLEAR:LV_URL,LS_BUTTON.
LV_SMARTFORM_NAME = 'ZREPAIR_REP01'.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL.
LS_BUTTON-TEXT = 'Repair Rep Print'. "#EC NOTEXT
LS_BUTTON-ON_CLIENT_CLICK = LV_URL.
LS_BUTTON-ENABLED = ABAP_TRUE. "#EC NOTEXT
INSERT LS_BUTTON INTO RT_BUTTONS INDEX . IF LV_PROCESS_TYPE = 'ZSV1'.
CLEAR:LV_URL,LS_BUTTON.
LV_SMARTFORM_NAME = 'ZCRM_SERVICE_01'.
CONCATENATE 'javascript:window.open( "/sap/bc/webdynpro/sap/ZCRM_PRINT' '?object_id=' LV_OBJECT_ID '&smartform_name=' LV_SMARTFORM_NAME '");' INTO LV_URL.
LS_BUTTON-TEXT = 'Repair Follow Print'. "#EC NOTEXT
LS_BUTTON-ON_CLIENT_CLICK = LV_URL.
LS_BUTTON-ENABLED = ABAP_TRUE. "#EC NOTEXT
INSERT LS_BUTTON INTO RT_BUTTONS INDEX .
ENDIF. ENDMETHOD.