![](/cvo_files/imgs_sap_abap/logos/logo_cvo_academy_it.png)
✒️ABAP La conversión de un Smartform a PDF
ABAP La conversión de un Smartform a PDF
![ABAP La conversión de un Smartform a PDF ABAP La conversión de un Smartform a PDF](https://www.cvosoft.com/cvo_files/imgs_sap_abap/og-images/414.jpg)
CONVERTING A SMARTFORM TO PDF
- STEP 1: Declaration of variables, structures, and internal tables we will work with.
* Nombre del modulo de funciones del formulario.
DATA: v_nombre_form(30) TYPE c.
DATA: ti_usuarios TYPE STANDARD TABLE OF zusuarios.
DATA: wa_control_parameters TYPE ssfctrlop,
wa_output_options TYPE ssfcompop,
wa_return TYPE ssfcrescl,
ti_otfdata LIKE itcoo OCCURS 0 WITH HEADER LINE,
ti_intab TYPE soli_tab,
ti_objbin TYPE solix_tab,
wa_intab TYPE LINE OF soli_tab,
wa_objbin TYPE LINE OF solix_tab,
ti_objpack TYPE STANDARD TABLE OF sopcklsti1 WITH HEADER LINE.
- STEP 2: Within the subroutine INVOCAR_SMARTFORM, we execute the function SSF_FUNCTION_MODULE_NAME with the name of the Smartform, which returns the function name of the Smartform.
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'ZFORMULARIO_PRUEBA'
IMPORTING
fm_name = v_nombre_form
EXCEPTIONS
no_form = 1
no_function_module = 2
OTHERS = 3.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
- STEP 3: Configuration of the output by setting three parameters:
* Configuramos la salida del formulario Smartform
wa_control_parameters-getotf = 'X'. "Retorno tab OTF Sin imprimir, visualizar
wa_control_parameters-no_dialog = 'X'. "Sin diálogo
wa_output_options-tdnoprev = 'X'. "Visualización de impresión no permitida
- STEP 4: Execution of the Smartform function using the output configuration structures filled in earlier, and also the structure wa_return, which stores the Smartform output in OTF format.
* Enviar datos al formulario e imprimirlo
CALL FUNCTION v_nombre_form
EXPORTING
output_options = wa_output_options
control_parameters = wa_control_parameters
IMPORTING
job_output_info = wa_return
TABLES
ti_usuarios = ti_usuarios
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
OTHERS = 5.
- STEP 5: Populating the OTFDATA table with the Smartform output in OTF format.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
ti_otfdata[] = wa_return-otfdata[].
ENDIF.
- STEP 6: Copying the content of the OTFDATA table to the INTAB table.
* En la tabla OTFDATA nos queda guardado el formulario en formato OTF
* Solo resta convertir el OTF a PDF
LOOP AT ti_otfdata.
wa_intab = ti_otfdata.
APPEND wa_intab TO ti_intab.
CLEAR wa_intab.
ENDLOOP.
- STEP 7: Execution of the function SX_OBJECT_CONVERT_OTF_PDF to convert the OTF format to PDF. The conversion result is stored in the objbin table.
* Convertir el OTF a PDF
CALL FUNCTION 'SX_OBJECT_CONVERT_OTF_PDF'
EXPORTING
format_src = 'OTF'
format_dst = 'PDF'
CHANGING
transfer_bin = ti_objpack-transf_bin
content_txt = ti_intab
content_bin = ti_objbin
objhead = ti_intab
len = ti_objpack-doc_size
EXCEPTIONS
err_conv_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.
- STEP 8: Execution of the GUI_DOWNLOAD method to download the PDF to our PC.
* En la tabla objbin nos queda guardado el PDF para salvarlo en un archivo local
* utilizando el metodo GUI_DOWNLOAD
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = 'C:Usersi0604226Desktopsalida.PDF'
filetype = 'BIN'
CHANGING
data_tab = ti_objbin[]
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
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
- Run our program.
There is another way to convert our Smartform into PDF format, which involves sending the Smartform printout to the SPOOL. This will generate a spool order number, then we must execute the standard program RSTXPDFT4, enter the spool order number in the selection screen, check the option to download PDF file, and enter the path and name of the PDF file to be generated. Finally, after entering this data, we will have obtained our Smartform in PDF format.
E.g
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_I_2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_i_2.
* Nombre del modulo de funciones del formulario.
DATA: v_nombre_form(30) TYPE c.
DATA: ti_usuarios TYPE STANDARD TABLE OF zusuarios.
DATA: wa_control_parameters TYPE ssfctrlop,
wa_output_options TYPE ssfcompop,
wa_return TYPE ssfcrescl,
ti_otfdata LIKE itcoo OCCURS 0 WITH HEADER LINE,
ti_intab TYPE soli_tab,
ti_objbin TYPE solix_tab,
wa_intab TYPE LINE OF soli_tab,
wa_objbin TYPE LINE OF solix_tab,
ti_objpack TYPE STANDARD TABLE OF sopcklsti1 WITH HEADER LINE.
* ---------------------------------------------------------------------*
START-OF-SELECTION.
* ---------------------------------------------------------------------*
* Seleccionamos los datos de la tabla de usuarios
PERFORM seleccionar_datos.
* Llamada al formulario Smartform
PERFORM invocar_smartform.
*&---------------------------------------------------------------------*
*& Form SELECCIONAR_DATOS
*&---------------------------------------------------------------------*
*& Cargar datos en la tabla interna
*&---------------------------------------------------------------------*
FORM seleccionar_datos .
CLEAR ti_usuarios.
REFRESH ti_usuarios.
SELECT dni nombre_ape estado_usu direccion
FROM ztabla_usuarios
INTO CORRESPONDING FIELDS OF TABLE ti_usuarios.
ENDFORM. " SELECCIONAR_DATOS
*&---------------------------------------------------------------------*
*& Form INVOCAR_SMARTFORM
*&---------------------------------------------------------------------*
*& Inicializar a imprimir smartform
*&---------------------------------------------------------------------*
FORM invocar_smartform .
IF NOT ti_usuarios[] IS INITIAL.
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'ZFORMULARIO_PRUEBA'
IMPORTING
fm_name = v_nombre_form
EXCEPTIONS
no_form = 1
no_function_module = 2
OTHERS = 3.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* Configuramos la salida del formulario Smartform
wa_control_parameters-getotf = 'X'. "Retorno tab OTF Sin imprimir, visualizar
wa_control_parameters-no_dialog = 'X'. "Sin diálogo
wa_output_options-tdnoprev = 'X'. "Visualización de impresión no permitida
* Enviar datos al formulario e imprimirlo
CALL FUNCTION v_nombre_form
EXPORTING
output_options = wa_output_options
control_parameters = wa_control_parameters
IMPORTING
job_output_info = wa_return
TABLES
ti_usuarios = ti_usuarios
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
OTHERS = 5.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
ti_otfdata[] = wa_return-otfdata[].
ENDIF.
* En la tabla OTFDATA nos queda guardado el formulario en formato OTF
* Solo resta convertir el OTF a PDF
LOOP AT ti_otfdata.
wa_intab = ti_otfdata.
APPEND wa_intab TO ti_intab.
CLEAR wa_intab.
ENDLOOP.
* Convertir el OTF a PDF
CALL FUNCTION 'SX_OBJECT_CONVERT_OTF_PDF'
EXPORTING
format_src = 'OTF'
format_dst = 'PDF'
CHANGING
transfer_bin = ti_objpack-transf_bin
content_txt = ti_intab
content_bin = ti_objbin
objhead = ti_intab
len = ti_objpack-doc_size
EXCEPTIONS
err_conv_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.
* En la tabla objbin nos queda guardado el PDF para salvarlo en un archivo local
* utilizando el metodo GUI_DOWNLOAD
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = 'C:Usersi0604226Desktopsalida.PDF'
filetype = 'BIN'
CHANGING
data_tab = ti_objbin[]
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
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDFORM. " INVOCAR_SMARTFORM
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 149 Publicaciones Académicas
🎓Egresado de los módulos:
- Carrera Consultor en SAP Fiori
- Carrera Consultor ABAP Nivel Avanzado
- Carrera Consultor ABAP Nivel Inicial
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez