Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SMART FORM TO PDF

Former Member
0 Kudos

HI ALL,

my requirement is to download a couple of timesheets of a single smartform into a pdf file[ each time sheet into one page]. how to deal with it. I dont have any idea . any suggestions are appreciated.

its urgent ...........

thanks in advance,

Suresh Aluri.

6 REPLIES 6

Former Member
0 Kudos

With ECC 5.0 you add new class available for sending mail without any trouble when you add document into the mail.

Please find below a code i wrote for a customer, i send Excel file with this one but you can adapt it to send PDF while using XSTRING.

Note that you have to configure SMTP communication for your SAP system .

Hope this will help you .

Best regards.

Report ZTESTBDE_MAIL

----


  • CLASS-DEFINITIONS *

----


DATA: send_request TYPE REF TO cl_bcs.

DATA: document TYPE REF TO cl_document_bcs.

DATA: sender TYPE REF TO cl_sapuser_bcs.

DATA: recipient TYPE REF TO if_recipient_bcs.

----


  • INTERNAL TABLES *

----


DATA: l_mailtext TYPE soli_tab.

DATA: l_mailhex TYPE solix_tab. " => to use with XSTRING Value

DATA: iaddsmtp TYPE bapiadsmtp OCCURS 0 WITH HEADER LINE.

DATA: ireturn TYPE bapiret2 OCCURS 0 WITH HEADER LINE.

----


  • VARIABLES *

----


DATA: mail_line LIKE LINE OF l_mailtext.

DATA: mailx_line LIKE LINE OF l_mailhex. " To use with Xsting only

DATA: bapiadsmtp TYPE bapiadsmtp.

  • Procedure

TRY.

  • Create persistent send request

send_request = cl_bcs=>create_persistent( ).

  • Get sender object

sender = cl_sapuser_bcs=>create( sy-uname ).

  • Add sender

CALL METHOD send_request->set_sender

EXPORTING

i_sender = sender.

  • Read the E-Mail address for the user

CALL FUNCTION 'BAPI_USER_GET_DETAIL'

EXPORTING

username = recipients_line-uname

TABLES

return = ireturn

addsmtp = iaddsmtp.

LOOP AT iaddsmtp WHERE std_no = 'X'.

CLEAR bapiadsmtp.

MOVE iaddsmtp TO bapiadsmtp.

ENDLOOP.

if bapiadsmtp-e_mail is initial.

  • No e-mail for user , add a default one if necessary.

bapiadsmtp-e_mail = c_defmail.

endif.

recipient = cl_cam_address_bcs=>create_internet_address( bapiadsmtp-e_mail ).

  • Add recipient with its respective attributes to send request

  • You can repeat this for all e-mail you want to send the mail , the system will

  • delete duplicates e-mail value

CALL METHOD send_request->add_recipient

EXPORTING

i_recipient = recipient

i_express = 'X'

i_copy = space

i_blind_copy = space

i_no_forward = space.

  • Set that you don't need a Return Status E-mail

CALL METHOD send_request->set_status_attributes

EXPORTING

i_requested_status = 'E'

i_status_mail = 'E'.

  • set send immediately flag - with this flag no job have to be define in SCOT to

  • send mail

send_request->set_send_immediately( 'X' ).

*set the subject of the mail

Subject = 'This is the subject of the mail'.

*Set text of the mail in table

mail_line = 'this the text of the mail'.

append mail_line to l_mailtext.

  • Build the document

document = cl_document_bcs->create_document(

i_type = 'RAW'

i_text = l_mailtext

i_subject = subject ).

  • Add attachement to document

call method document->add_attachement

exporting

i_attachement_type = 'CSV'

i_attachement_subject = 'Sample_Excel_File'

i_att_content_text = it_file. " U should replace it by PDF file in Hexa

  • Add document to send request

call method send_request->set_document( document ).

  • Send document

call method send_request->send( ).

commit work.

catch cx_send_req_bcs into w_except.

call method w_except->if_message~get_longtext

receiving

result = text.

catch cx_address_bcs into w_except.

call method w_except->if_message~get_longtext

receiving

result = text.

catch cx_document_bcs into w_except.

call method w_except->if_message~get_longtext

receiving

result = text.

endtry.

ata : file_to_send TYPE xstring ,

pdf_document TYPE solix_tab .

*Convert Smartforms into PDF

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

format = 'PDF'

max_linewidth = 132

IMPORTING

bin_filesize = file_size

bin_file = file_to_send

TABLES

otf = tab_otf

lines = i_tline

EXCEPTIONS

err_max_linewidth = 1

err_format = 2

err_conv_not_possible = 3

OTHERS = 4.

  • add attachment to document

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

EXPORTING

buffer = file_to_send

TABLES

binary_tab = pdf_document.

CALL METHOD document->add_attachment

EXPORTING

i_attachment_type = 'PDF'

i_attachment_subject = att_subject

i_att_content_hex = pdf_document.

For PDF file , you should use the method ADD_ATTACHEMENT like this :

CALL METHOD document->add_attachement

EXPORTING

i_attachement_type = 'PDF'

i_attachement_subject = 'This is a PDF File' " it's a sample name

i_att_content_hex = content_hex "

Content_Hex should contains your PDF file in hexadecimals .

0 Kudos

hi,

this not related to want i want. any way thanks for ur reply.

with regards,

Suresh ALuri.

Former Member
0 Kudos

hi

good

go through this code hope this ll help you to solve your problem

As you are capturing the OTF data from Smartform itself, try using Convert_otf and then GUI_DOWNLOAD Function Modules to download as pdf.

I am sending you the code. Please check your code against it and still if you have any doubts i ll clarify it. You can just copy paste this code and check it.

DATA: form_name TYPE rs38l_fnam, " Used to get the function module of Smartform

wa_ctrlop TYPE ssfctrlop, " Smart Forms: Control structure

wa_outopt TYPE ssfcompop, " SAP Smart Forms: Smart Composer (transfer) options

t_otfdata TYPE ssfcrescl. " Smart Forms: Return value at end of form printing

Data: t_pdf_tab type table of tline, " SAPscript: Text Lines

t_otf TYPE table of itcoo. " OTF Structure

  • Variables used to pass to GUI_DOWNLOAD

DATA: w_filesize TYPE i,

w_bin_filesize TYPE i.

  • Variables used for Save Dialog Box

DATA : file_name TYPE string,

file_path TYPE string,

full_path TYPE string.

START-OF-SELECTION.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'

EXPORTING

formname = 'ZPDF_G' "p_name

IMPORTING

fm_name = form_name

EXCEPTIONS

no_form = 1

no_function_module = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

wa_ctrlop-getotf = 'X'.

wa_ctrlop-no_dialog = 'X'.

wa_outopt-tdnoprev = 'X'.

CALL FUNCTION form_name

EXPORTING

control_parameters = wa_ctrlop

output_options = wa_outopt

user_settings = 'X'

IMPORTING

job_output_info = t_otfdata

EXCEPTIONS

formatting_error = 1

internal_error = 2

send_error = 3

user_canceled = 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.

END-OF-SELECTION.

t_otf[] = t_otfdata-otfdata[].

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

format = 'PDF'

max_linewidth = 132

IMPORTING

bin_filesize = w_bin_filesize

TABLES

otf = t_otf

lines = t_pdf_tab

EXCEPTIONS

err_max_linewidth = 1

err_format = 2

err_conv_not_possible = 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.

----


TAKING THE DOWNLOAD FILE PATH AS USER INPUT*

CALL METHOD cl_gui_frontend_services=>file_save_dialog

CHANGING

filename = file_name

path = file_path

fullpath = full_path

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

not_supported_by_gui = 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.

  • Download the file to the selected path

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

bin_filesize = w_bin_filesize

filename = full_path "fname1

filetype = 'BIN'

IMPORTING

filelength = w_filesize

TABLES

data_tab = t_pdf_tab

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

no_authority = 5

unknown_error = 6

header_not_allowed = 7

separator_not_allowed = 8

filesize_not_allowed = 9

header_too_long = 10

dp_error_create = 11

dp_error_send = 12

dp_error_write = 13

unknown_dp_error = 14

access_denied = 15

dp_out_of_memory = 16

disk_full = 17

dp_timeout = 18

file_not_found = 19

dataprovider_exception = 20

control_flush_error = 21

OTHERS = 22.

IF sy-subrc <> 0.

MESSAGE i000(zpdf). "File not downloaded

ELSE.

MESSAGE i001(zpdf). "File downloaded

ENDIF.

reward point if helpful.

thanks

mrutyun^

0 Kudos

hi,

thanks for ur reply.im getting the pdf file into my desktop but when i try to open it it giving me an error as ' FILE CORRUPTED'. how to deal with it.

if possible check my code.

IF it_itab IS NOT INITIAL.

  • GETTING THE SMART FORM NAME

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' " for calling smartform YJJ_SMARTFORM

EXPORTING

formname = 'YJJ_SMARTFORM'

IMPORTING

fm_name = funname.

  • Calling the smartforms FUNNAME

CALL FUNCTION FUNNAME

EXPORTING

  • ARCHIVE_INDEX =

  • ARCHIVE_INDEX_TAB =

  • ARCHIVE_PARAMETERS =

CONTROL_PARAMETERS = st_control_parameters

  • MAIL_APPL_OBJ =

  • MAIL_RECIPIENT =

  • MAIL_SENDER =

OUTPUT_OPTIONS = st_output_options

  • USER_SETTINGS = 'X'

controlno = IT_ITAB-CONTROLNO

pernr = IT_ITAB-PERNR

sname = IT_ITAB-SNAME

stat = IT_ITAB-STAT

  • KOSTL =

werks = IT_ITAB-WERKS

date = P_DATE

  • IMPORTING

  • DOCUMENT_OUTPUT_INFO =

JOB_OUTPUT_INFO = tab_otf_data

  • JOB_OUTPUT_OPTIONS =

  • EXCEPTIONS

  • FORMATTING_ERROR = 1

  • INTERNAL_ERROR = 2

  • SEND_ERROR = 3

  • USER_CANCELED = 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.

*APPENDING OTF DATA INTO FINAL TABLE.

tab_otf_final[] = tab_otf_data-otfdata[].

  • calling the smartform FUNNAME within a loop for multiple records

LOOP AT IT_ITAB.

  • Calling the smartforms FUNNAME

CALL FUNCTION FUNNAME

EXPORTING

  • ARCHIVE_INDEX =

  • ARCHIVE_INDEX_TAB =

  • ARCHIVE_PARAMETERS =

CONTROL_PARAMETERS = st_control_parameters

  • MAIL_APPL_OBJ =

  • MAIL_RECIPIENT =

  • MAIL_SENDER =

OUTPUT_OPTIONS = st_output_options

  • USER_SETTINGS = 'X'

controlno = IT_ITAB-CONTROLNO

pernr = IT_ITAB-PERNR

sname = IT_ITAB-SNAME

stat = IT_ITAB-STAT

  • KOSTL =

werks = IT_ITAB-WERKS

date = P_DATE

  • IMPORTING

  • DOCUMENT_OUTPUT_INFO =

JOB_OUTPUT_INFO = tab_otf_data

  • JOB_OUTPUT_OPTIONS =

  • EXCEPTIONS

  • FORMATTING_ERROR = 1

  • INTERNAL_ERROR = 2

  • SEND_ERROR = 3

  • USER_CANCELED = 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.

  • removing initial and final markers from otf file

DELETE TAB_OTF_DATA-OTFDATA WHERE tdprintcom = '//'.

  • SEARCHING FOR END OF PAGE IN OTF FILE

READ TABLE tab_otf_final WITH KEY tdprintcom = 'EP'.

my_tabix = sy-tabix + 1.

  • APPENDING MODIFIED OTF FILE TO FINAL TABLE

INSERT LINES OF tab_otf_data-otfdata INTO tab_otf_final INDEX my_tabix.

ENDLOOP.

ADDING FINAL MARKER FOR FINAL TABLE

TAB_OTF_FINAL-tdprintcom = '//'.

APPEND TAB_OTF_FINAL.

  • converting otfdata into pdf data

CALL FUNCTION 'CONVERT_OTF_2_PDF'

IMPORTING

bin_filesize = bin_filesize

TABLES

otf = tab_otf_final

doctab_archive = it_docs

lines = it_lines

EXCEPTIONS

err_conv_not_possible = 1

err_otf_mc_noendmarker = 2

OTHERS = 3.

CREATE OBJECT v_guiobj.

CALL METHOD v_guiobj->file_save_dialog

EXPORTING

default_extension = 'PDF'

default_file_name = v_name

file_filter = v_filter

CHANGING

filename = w_file

path = v_path

fullpath = v_fullpath

user_action = v_uact.

IF v_uact = v_guiobj->action_cancel.

EXIT.

ENDIF.

ENDIF.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

bin_filesize = bin_filesize

filename = w_file

filetype = 'BIN'

  • APPEND = ' '

  • WRITE_FIELD_SEPARATOR = ' '

  • HEADER = '00'

  • TRUNC_TRAILING_BLANKS = ' '

  • WRITE_LF = 'X'

  • COL_SELECT = ' '

  • COL_SELECT_MASK = ' '

  • DAT_MODE = ' '

  • CONFIRM_OVERWRITE = ' '

  • NO_AUTH_CHECK = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • WRITE_BOM = ' '

  • TRUNC_TRAILING_BLANKS_EOL = 'X'

  • IMPORTING

  • FILELENGTH =

TABLES

data_tab = it_lines

  • FIELDNAMES =

  • EXCEPTIONS

  • FILE_WRITE_ERROR = 1

  • NO_BATCH = 2

  • GUI_REFUSE_FILETRANSFER = 3

  • INVALID_TYPE = 4

  • NO_AUTHORITY = 5

  • UNKNOWN_ERROR = 6

  • HEADER_NOT_ALLOWED = 7

  • SEPARATOR_NOT_ALLOWED = 8

  • FILESIZE_NOT_ALLOWED = 9

  • HEADER_TOO_LONG = 10

  • DP_ERROR_CREATE = 11

  • DP_ERROR_SEND = 12

  • DP_ERROR_WRITE = 13

  • UNKNOWN_DP_ERROR = 14

  • ACCESS_DENIED = 15

  • DP_OUT_OF_MEMORY = 16

  • DISK_FULL = 17

  • DP_TIMEOUT = 18

  • FILE_NOT_FOUND = 19

  • DATAPROVIDER_EXCEPTION = 20

  • CONTROL_FLUSH_ERROR = 21

  • OTHERS = 22

.

IF sy-subrc EQ 0.

MESSAGE 'PDF FILE DOWNLOADED' TYPE 'S'.

ENDIF.

ENDFORM. "dispdownload

thanks n regards,

Suresh Aluri.

Former Member
0 Kudos

Hi

http://sap4.com/wiki/index.php?title=Portada

Please check this code and replace the variables as per ur reqt.

This is the code which is working perfectly .

  • event handler for checking and processing user input and

  • for defining navigation

DATA: cached_response TYPE REF TO if_http_response.

DATA: response TYPE REF TO if_http_response.

DATA: guid TYPE guid_32.

  • generated result: PDF format

DATA: l_pdf_xstring TYPE xstring,

lt_lines TYPE TABLE OF tline,

ls_line TYPE tline,

l_pdf_len TYPE i.

DATA: lf_fm_name TYPE rs38l_fnam ,

e_ssfcrescl TYPE ssfcrescl,

lv_spool TYPE tsfspoolid,

e_ssfctrlop TYPE ssfctrlop,

e_ssfcompop TYPE ssfcompop.

DATA w_printer TYPE rspopname."user01-spld.

DATA:l_doc_output_info TYPE ssfcrespd, "#EC NEEDED

l_job_output_info TYPE ssfcrescl, "#EC NEEDED

l_job_output_options TYPE ssfcresop. "#EC NEEDED

DATA :l_devtype TYPE rspoptype.

  • Get the function module name of Smartform

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'

EXPORTING

formname = '/BSHP/FW_SF_LIST_DEL'

variant = ' '

direct_call = ' '

IMPORTING

fm_name = lf_fm_name "/1BCDWB/SF00000084

EXCEPTIONS

no_form = 1

no_function_module = 2

OTHERS = 3.

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 print parameters

e_ssfctrlop-no_dialog = 'X' .

*----


  • Setting of output options

*----


  • language

language = sy-langu.

TRANSLATE language TO UPPER CASE.

e_ssfctrlop-langu = language.

  • set control parameters to get the output format (OTF) from Smart Forms

e_ssfctrlop-no_dialog = 'X'.

e_ssfctrlop-getotf = 'X'.

  • get device type from language

CALL FUNCTION 'SSF_GET_DEVICE_TYPE'

EXPORTING

i_language = language

  • i_application = 'SAPDEFAULT'

IMPORTING

e_devtype = l_devtype

EXCEPTIONS

no_language = 1

language_not_installed = 2

no_devtype_found = 3

system_error = 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.

ENDIF.

  • set device type in output options

e_ssfcompop-tdprinter = l_devtype.

  • Call the func module of the Smartform

CALL FUNCTION lf_fm_name " '/1BCDWB/SF00000084'

EXPORTING

control_parameters = e_ssfctrlop

output_options = e_ssfcompop

user_settings = space

  • sf_werks = p_werks

sf_lifnr = p_lifnr

sf_matnr_low = s_matnr_low

sf_matnr_high = s_matnr_high

sf_vbeln_low = s_vbeln_low

sf_vbeln_high = s_vbeln_high

sf_lfdat_low = s_lfdat_low

sf_lfdat_high = s_lfdat_high

sf_vgbel_low = s_vgbel_low

sf_vgbel_high = s_vgbel_high

IMPORTING

document_output_info = l_doc_output_info

job_output_info = l_job_output_info

job_output_options = l_job_output_options

TABLES

gt_final_fi = gt_final

EXCEPTIONS

formatting_error = 1

internal_error = 2

send_error = 3

user_canceled = 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.

*----


  • Conversion of output format OTF into PDF format

*----


  • now convert the final document (OTF format) into PDF format

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

format = 'PDF'

  • MAX_LINEWIDTH = 132

  • ARCHIVE_INDEX = ' '

  • COPYNUMBER = 0

IMPORTING

bin_filesize = l_pdf_len

bin_file = l_pdf_xstring " binary file

TABLES

otf = l_job_output_info-otfdata

lines = lt_lines

EXCEPTIONS

err_max_linewidth = 1

err_format = 2

err_conv_not_possible = 3

err_bad_otf = 4

OTHERS = 5 .

IF sy-subrc EQ 0.

CREATE OBJECT cached_response TYPE cl_http_response EXPORTING add_c_msg = 1.

l_pdf_len = XSTRLEN( l_pdf_xstring ).

cached_response->set_data( data = l_pdf_xstring

length = l_pdf_len ).

cached_response->set_header_field( name = if_http_header_fields=>content_type

value = 'application/pdf' ).

cached_response->set_status( code = 200 reason = 'OK' ).

cached_response->server_cache_expire_rel( expires_rel = 180 ).

CALL FUNCTION 'GUID_CREATE'

IMPORTING

ev_guid_32 = guid.

CONCATENATE runtime->application_url '/' guid '.pdf' INTO display_url.

cl_http_server=>server_cache_upload( url = display_url

response = cached_response ).

ENDIF.

U will get the url of the pdf in the display_url

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/convert%2bsmartform%2bto%2bpdf%2bformat

<b>Reward if usefull</b>

Former Member
0 Kudos

answered