Skip to Content
Author's profile photo Former Member

How to download assembled document (DOCX) from SAP Document Builder.

report  z_download_docx.

parameters: p_docid like /ipro/tdocmntdocmnt_id.

parameters: p_cntnt like /ipro/tdocmntcontent.

parameters: p_outdf like /ipro/toutdefotype.

* implicit constant definition for context node content

types:

   begin of element_content,

     content  type /ipro/tcontentcontent,

     content_object  type ref to /ipro/if_content,

     content_lbl  type string,

   end of element_content,

   elements_content type

      standard table of element_content

      with default key.

data:

   lt_content      type elements_content,

   lt_contents     type /ipro/tt_contents,

   lo_content      type ref to /ipro/if_content,

   ls_sel_content  like line of lt_content,

   lr_docb_factory type ref to /ipro/if_docb_factory,

   lr_model        type ref to /ipro/if_model,

   lx_notfound     type ref to /ipro/cx_serializer_failed_at,

   lo_output               type ref to /ipro/if_output.

data: piv_name type string.

move p_cntnt to piv_name.

call function ‘/IPRO/CONTENT_FACTORY’

   exporting

     piv_name     = piv_name

   importing

     pet_contents = lt_contents.

read table lt_contents into lo_content index 1.

lr_docb_factory = lo_content->get_docb_factory( ).

check lr_docb_factory is not initial.

lr_model = lr_docb_factory->lookup( piv_docmnt_id = p_docid

                                     piv_version = 1 ).

if lr_model is initial.

   try.

       lr_model ?=

         lr_docb_factory->open( piv_docmnt_id = p_docid

                                piv_docmnt_version = 1 ).

     catch /ipro/cx_serializer_failed_at into lx_notfound.

       exit.

     catch /ipro/cx_action_noauth

           /ipro/cx_action_illegal.

       exit.

   endtry.

endif.

if lr_model is bound.

   lo_output ?= lr_model.

endif.

data: ls_doc                  type /ipro/s_output.

if lo_output is bound.

   try.

       lo_output->enable( p_outdf ).

       ls_doc = lo_output->generate( p_outdf ).

     catch /ipro/cx_output_otype_invalid.

     catch /ipro/cx_output_gen_failed.

   endtry.

endif.

data:

   lv_file_len type i,

   out_tab     type standard table of tbl1024.

call function ‘SCMS_XSTRING_TO_BINARY’

   exporting

     buffer          = ls_docdata

*   APPEND_TO_TABLE = ‘ ‘

   importing

     output_length   = lv_file_len

   tables

     binary_tab      = out_tab.

call function ‘GUI_DOWNLOAD’

   exporting

     bin_filesize = lv_file_len

     filename     = ‘d:\1.docx.’

     filetype     = ‘BIN’

   tables

     data_tab     = out_tab

   exceptions

     others       = 22.

if sysubrc <> 0.

* Implement suitable error handling here

endif.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.