Vorraussetzungen
Für das folgende Projekt wird ein WAS in der Version
6.10 benötigt. Dieser kann als Evaluierungsversion für Linux
kostenlos unter der Adresse:
http://www.sap.com/solutions/technology/linux/eval/was/
bestellt werden. Die beschriebene Funktion setzt aber
zusätzlich Support Package 20 vorraus, da hier der Baustein CONVERT_OTF um
eine Binärausgabe des PDF Dokuments erweitert wurde.
Vorbereitung
Dieses Beispiel für die dynamische PDF generierung mit
dem WAS basiert auf der BSP Applikation SF_WEBFORM_01 die nach Z_SF_WEBFORM_01
kopiert wurde.
Implementation
Layout: start.htm
Das Layout der Seite start.htm wurde um die Auswahl des
Ausgabeformats ergänzt:
</tr>
<td><otr>Ausgabeformat</otr></td>
<td colspan=4><select name="outputformat" size=1>
<option> PDF
<option> HTML
</select></td>
</tr>
Eventhandler OnInputProcessing: start.htm
Der Parameter outputformat muß über den
Eventhandler OnInputProcessing übergeben werden:
case event_id.
when 'display'.
navigation->set_parameter( 'form_name' ).
navigation->set_parameter( 'language' ).
navigation->set_parameter( 'carrier_low' ).
navigation->set_parameter( 'carrier_high' ).
navigation->set_parameter( 'customer_id' ).
navigation->set_parameter( 'outputformat' ).
navigation->next_page( 'DISPLAY_FORM' ).
endcase.
Seitenattribute
Die Seitenattribute der Seite form.htm müssen um
outputformat ergänzt werden:
Eventhandler OnInputProcessing: form.htm
Eventhandler für die PDF oder HTML Generierung:
* data to select
data: lt_customer type scustom,
lt_bookings type ty_bookings,
lt_connections type ty_connections.
* self defined select-options table
data: lt_scar type table of car_sopt,
ls_scar type car_sopt.
* parameters for generated function module
data: l_function_module_name type rs38l_fnam.
data: ls_output_options type ssfcompop,
ls_control_parameters type ssfctrlop.
data: ls_output_data type ssfcrescl.
* generated result: HTML with embedded CSS
data: ls_xmloutput type ssfxmlout,
lt_html_raw type tsfixml.
data: l_xstring type xstring, "needed for HTTP response
l_xlength type i,
l_html_xstring type xstring.
* generated result: OTFDATA
data: lt_otfdata type tsfotf.
* converted result: PDF Data.
data: lt_pdfdata type table of tline,
ls_pdfdata like line of lt_pdfdata.
* Outputvariables for the PDF Output
data: l_string type string, "needed for HTTP response
l_length type i,
l_html_string type string.
*--------------------------------------------------------------
* For debugging all Page Attributes must be set
*--------------------------------------------------------------
if carrier_high is initial.
carrier_high = 'AA'.
endif.
if carrier_low is initial.
carrier_low = 'AA'.
endif.
if customer_id is initial.
customer_id = 6.
endif.
if form_name is initial.
form_name = 'SF_EXAMPLE_02'.
endif.
if language is initial.
language = 'D'.
endif.
if outputformat is initial.
outputformat = 'PDF'.
endif.
*--------------------------------------------------------------
* Data selection
*--------------------------------------------------------------
* Nothing will be selected if the user entered lower case,
* so translate it:
translate carrier_low to upper case.
translate carrier_high to upper case.
* fill select-options:
ls_scar-sign = 'I'.
ls_scar-option = 'BT'.
ls_scar-low = carrier_low.
ls_scar-high = carrier_high.
append ls_scar to lt_scar.
* select data or return if no customer exists
select single * from scustom into lt_customer where id = customer_id.
check sy-subrc eq 0.
select * from sbook into table lt_bookings
where customid = customer_id
and carrid in lt_scar
order by primary key.
select * from spfli into table lt_connections
for all entries in lt_bookings
where carrid = lt_bookings-carrid
and connid = lt_bookings-connid
order by primary key.
*-------------------------------------------
* get name of generated function module
*---------------------------------------
call function 'SSF_FUNCTION_MODULE_NAME'
exporting formname = form_name
* variant = ' '
* direct_call = ' '
importing fm_name = l_function_module_name
exceptions no_form = 1
no_function_module = 2
others = 3.
if sy-subrc <> 0.
* error handling
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
exit.
endif.
*-----------------------------------------------------------
* activate XSF Output Mode
*---------------------------
if outputformat = 'PDF'.
** Deactivate XDF
* ls_output_options-xdfcmode = 'X'.
* ls_output_options-xdf = space.
** Deactivate XSF and XSF + HTML
* ls_output_options-xsfcmode = 'X'.
* ls_output_options-xsf = space.
** Deactivate Dialog and only get OTF Data
ls_control_parameters-no_dialog = 'X'.
ls_control_parameters-getotf = 'X'.
else.
ls_output_options-xsf = 'X'. " XSF Output active
ls_output_options-xsfcmode = 'X'. " Get XSF params from program
ls_output_options-xsfoutmode = 'A'. " A:Application, S:Spool
ls_output_options-xsfformat = 'X'. " Formatting ON
ls_output_options-xsfaction = space. " Action URL for submit btn.
endif.
* language
translate language to upper case.
ls_control_parameters-langu = language.
*------------------------------------------------------------
* Call the generated function module
*---------------------------------------
call function l_function_module_name
exporting
* archive_index =
* archive_parameters =
control_parameters = ls_control_parameters
* mail_appl_obj =
* mail_recipient =
* mail_sender =
output_options = ls_output_options
user_settings = space
customer = lt_customer
bookings = lt_bookings
connections = lt_connections
importing
* document_output_info =
job_output_info = ls_output_data
* job_output_options =
exceptions formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
others = 5.
if sy-subrc <> 0.
* error handling
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
exit.
endif.
if outputformat = 'PDF'.
*--------------------------------------------------------------------
* PDF ausgabe
*--------------------------------------------------------------------
lt_otfdata = ls_output_data-otfdata.
call function 'CONVERT_OTF'
exporting
format = 'PDF'
* MAX_LINEWIDTH = 132
* ARCHIVE_INDEX = ' '
* COPYNUMBER = 0
importing
bin_filesize = l_xlength
bin_file = l_html_xstring
tables
otf = lt_otfdata
lines = lt_pdfdata
exceptions
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
err_bad_otf = 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.
*--------------------------------------------------------------------
* Fill HTTP request
*--------------------------------------------------------------------
response->set_header_field( name = 'content-type'
value = 'application/pdf' ).
l_xlength = xstrlen( l_html_xstring ).
response->set_data( data = l_html_xstring
length = l_xlength ).
else.
********************************************************************
ls_xmloutput = ls_output_data-xmloutput.
lt_html_raw = ls_xmloutput-trfresult-content[].
*--------------------------------------------------------------------
* Fill HTTP request
*--------------------------------------------------------------------
response->set_header_field( name = 'content-type'
value = ls_xmloutput-trfresult-type ).
*-------------
* SAP Smart Forms returns XML data island in raw data format.
* method 'set_data' of the response object needs the output
* in XSTRING. The next loop convertes the raw table into xstring.
*-------------
loop at lt_html_raw into l_xstring.
concatenate l_html_xstring l_xstring into l_html_xstring
in byte mode.
endloop.
l_xlength = xstrlen( l_html_xstring ).
response->set_data( data = l_html_xstring
length = l_xlength ).
*---------------------------------------------------------------------
endif. |