Application Development and Automation 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: 
Read only

How to Autoprint PDF files?

Former Member
0 Likes
4,878

Hi there experts!

I've read some articles and discussions on forums  about auto-printing pdf files and i've tried some things but with no success.

My task is that i need to print pdf files from local or server without  the need of seeing the files.

what i understand is that i need 2 objects , one of tipe cl_gui_custom_container and one of cl_gui_pdfviewer. But i don't really know how to use them to make my program work.

this is my code so far. i don't get any error but i don't get any print either.

DATA url_doc TYPE char255.

DATA pdf_file TYPE REF TO cl_gui_pdfviewer.

DATA pdf_container TYPE REF TO cl_gui_custom_container.

CALL FUNCTION 'SET_PRINT_PARAMETERS'

   EXPORTING

     DESTINATION                 = 'LOCL'.

CREATE OBJECT pdf_container

   EXPORTING

     container_name              = 'PDF'

   EXCEPTIONS

     others                      = 1

     .

IF sy-subrc <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CREATE OBJECT pdf_file

   EXPORTING

     parent            = pdf_container

   EXCEPTIONS

     others            = 1.

IF sy-subrc <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CONCATENATE 'file://' v_file into url_doc.

*

pdf_file->open_document( url = url_doc ).

IF sy-subrc <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CALL METHOD pdf_file->print

    EXCEPTIONS

     cntl_error        = 1

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

PS: v_file is the path of my file like C:/temp/abcd.pdf

1 ACCEPTED SOLUTION
Read only

Jelena_Perfiljeva
Active Contributor
0 Likes
4,138

Just a suggestion - maybe if you could elaborate more on what is the business purpose of the program, someone could suggest an alternative solution. Not sure it makes sense to use SAP just for reading/printing files (which is a basic function of OS). But I can only guess there is more to it... Where are the files coming from, why do they need to be printed and why from SAP?

Hi there experts!

I've read some articles and discussions on forums  about auto-printing pdf files and i've tried some things but with no success.

My task is that i need to print pdf files from local or server without  the need of seeing the files.

what i understand is that i need 2 objects , one of tipe cl_gui_custom_container and one of cl_gui_pdfviewer. But i don't really know how to use them to make my program work.

this is my code so far. i don't get any error but i don't get any print either.

DATA url_doc TYPE char255.

DATA pdf_file TYPE REF TO cl_gui_pdfviewer.

DATA pdf_container TYPE REF TO cl_gui_custom_container.

CALL FUNCTION 'SET_PRINT_PARAMETERS'

   EXPORTING

     DESTINATION                 = 'LOCL'.

CREATE OBJECT pdf_container

   EXPORTING

     container_name              = 'PDF'

   EXCEPTIONS

     others                      = 1

     .

IF sy-subrc <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CREATE OBJECT pdf_file

   EXPORTING

     parent            = pdf_container

   EXCEPTIONS

     others            = 1.

IF sy-subrc <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CONCATENATE 'file://' v_file into url_doc.

*

pdf_file->open_document( url = url_doc ).

IF sy-subrc <> 0.

  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CALL METHOD pdf_file->print

    EXCEPTIONS

     cntl_error        = 1

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

PS: v_file is the path of my file like C:/temp/abcd.pdf

19 REPLIES 19
Read only

Former Member
0 Likes
4,138

Hi Alex,

If it is just for Local printing i would use one of the available PDF batch printing utility like the below;

http://www.robvanderwoude.com/printfiles.php#PrintPDF

Regards,

Karthik D

Read only

0 Likes
4,138

Hi Karthik,

Thx for the quick response. I will try and see if i understand what you are telling me. I never used batch utilities in ABAP, i don't know even what they are but i'll go look now for them and i'll come back with a response if this helped me.

Thanks again,

Alexandru F

Read only

0 Likes
4,138

Sorry is not really what i need. it must be independent of OS. And this can be used only on windows. But thx a lot. At least now i know how to call files . I didn't know how to use though. I don't understand what  i have to send to parameters? the path of the file?

Read only

0 Likes
4,138

Hi Alex,

You have to first install(nothing more than just copying) the utility to the Windows System folder. Then through ABAP program you have to construct and download another .bat file to some local folder with the contents as below;

PRINTPDF c:\pdffolder\*.pdf

Now execute the downloaded file using the execute function so that the mentioned files will be automatically printed.

I have not tested this and not sure that it would work. But given an opportunity i would work this out as mentioned above only.

Regards,

Karthik D

Read only

0 Likes
4,138

but there should be an approach using cl_gui_pdfviewer or cl_gui_html_viewer. But i can't find it.

I tried with both and had no success.

with html viewer and even with pdf_viewer i get the path do the doc. And if i issue the print method nothing happens. Is there any example using any of the above class for printing?

Read only

0 Likes
4,138

You can use this in foreground:

CALL METHOD cl_gui_frontend_services=>execute

EXPORTING

application = 'AcroRd32.exe'

parameter = '/p /h C:\YourPDFile.pdf'    "P = Print, h = Hide Window

minimized = 'X'

operation = ''

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

bad_parameter = 3

file_not_found = 4

path_not_found = 5

file_extension_unknown = 6

error_execute_failed = 7

synchronous_failed = 8

not_supported_by_gui = 9

OTHERS = 10 .

Read only

0 Likes
4,138

this is ok, but i think it's usable only on OS Windows, no?

Read only

0 Likes
4,138

Yes, what are your expectations?

If you want to do this in background, you will have to make use of OS commands.SM69.

The same command can be use there in OS commands i.e. AcroRd32 /p /h C:\YourPDFile.pdf.

For running the command you can use FM SXPG_CALL_SYSTEM.

http://help.sap.com/saphelp_40b/helpdata/en/fa/0971ee543b11d1898e0000e8322d00/content.htm

I have not tested this yet, i will check this tomorrow and let you know.

Read only

0 Likes
4,138

Let's say i might not have windows on computer. i have unix , or OS. than i will not have acroRD32.exe. I need a program that will work independently  from the OS i have. That's way i was asking for an example how i can use cl_gui_pdfviewer or cl_gui_html_viewer to print the documents. Is it possible?

Thanks a lot for all your hard work though. I learned new things

Read only

0 Likes
4,138

Hi,

   Use GET_PLATFORM method of CL_GUI_FRONTEND_SERVICES class to get the OS, if windows use windows logic, if unix use below method

DATA : v_document  TYPE string,
           v_application TYPE string.


v_document = '/tmp/pdffile.pdf'.  " give the file name correctly.

v_application = '/usr/bin/acroread'.  


    CALL METHOD cl_gui_frontend_services=>execute
    EXPORTING
      document               = v_document
      application             = v_application
*     PARAMETER              =
*     DEFAULT_DIRECTORY      =
*     MAXIMIZED              =
*     MINIMIZED              =
*     SYNCHRONOUS            =
      operation              = 'OPEN'
    EXCEPTIONS
      cntl_error             = 1
      error_no_gui           = 2
      bad_parameter          = 3
      file_not_found         = 4
      path_not_found         = 5
      file_extension_unknown = 6
      error_execute_failed   = 7
      synchronous_failed     = 8
      not_supported_by_gui   = 9
      OTHERS                 = 10.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

Thanks & Regards

Bala Krishna

Read only

Jelena_Perfiljeva
Active Contributor
0 Likes
4,139

Just a suggestion - maybe if you could elaborate more on what is the business purpose of the program, someone could suggest an alternative solution. Not sure it makes sense to use SAP just for reading/printing files (which is a basic function of OS). But I can only guess there is more to it... Where are the files coming from, why do they need to be printed and why from SAP?

Read only

0 Likes
4,138

Is more as an exercise. Just select a pdf form local ( my computer let's say ) or application server. and print it without the need to see it. I need to send it automatically to the local printer. i don't think i can use the

CALL METHOD cl_gui_frontend_services=>execute

EXPORTING

application = 'AcroRd32.exe'

Because is dependent on my computer. I have to use only SAP to do this, maybe there is a module of SAP that has acrobat functions of reading / printing pdf. That is what i need. I hope i made my problem more clear, i'm not sure how i can explain it better.

Thank you,

Alexandru Fediuc

Read only

0 Likes
4,138

Hi Alexandru,

Check this link.

http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=132351376

Here the author shows how to use CL_GUI_HTML_CONTAINER and print from the same window.

He is using an HTML page for his document, you can change to PDF and try it out.

What I have seen is that the Printer Dialog is being popped up for this Class.

I also tried with PDFVIEWER, but I am not able to print, eventhough it has a methof Print available.

Explore and let me know.

Thanks,

Shambu

Read only

0 Likes
4,138

I'll look more into it. It looks promising.

Thank you,

Alexandru F

Read only

0 Likes
4,138

Hi Alex,

When you use cl_gui_html_viewer you can write code as follows to print the PDF;

*   Flush
     CALL METHOD cl_gui_cfw=>flush.
*   PRINT
     CALL METHOD html_viewer->execwb
                   EXPORTING cmd_id  = html_viewer->wb_cmdid_print
                   EXCEPTIONS cntl_error = 1.
     IF sy-subrc <> 0.
       MESSAGE e003(cnht) RAISING html_print_error.
     ENDIF.

But the only issue is the Print Dialog, i tried several way but could not suppress it.

Regards,

Karthik D

Read only

0 Likes
4,138

I think it's ok with printing dialog to. I'm doing something wrong if for you works... Can you help me a bit?

DATA url_doc TYPE char255.

DATA pdf_file TYPE REF TO cl_gui_html_viewer.

DATA pdf_container TYPE REF TO cl_gui_custom_container.

*Crate Custom Container

CREATE OBJECT pdf_container

   EXPORTING

     container_name = 'PDF_FILE'

   EXCEPTIONS

     OTHERS         = 1.

IF sy-subrc <> 0.

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

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

ENDIF.

*Crate html viewer object

CREATE OBJECT pdf_file

   EXPORTING

     parent = pdf_container

   EXCEPTIONS

     OTHERS = 1.


*Crate url // v_file is the path of the file , like C:/temp/test.pdf

CONCATENATE 'file://' v_file INTO url_doc.

*Add the url to the object

CALL METHOD pdf_file->show_url

   EXPORTING

     url = url_doc.

*Now i added your code

*   Flush

CALL METHOD cl_gui_cfw=>flush.

*   print

CALL METHOD pdf_file->execwb

   EXPORTING

     cmd_id     = pdf_file->wb_cmdid_print

   EXCEPTIONS

     cntl_error = 1.

IF sy-subrc <> 0.

   MESSAGE e003(cnht) RAISING html_print_error.

ENDIF.


Thank you,

Alexandru F


Read only

0 Likes
4,138

Hi Alex,

I just copied the program RSDEMO_HTML_VIEWER to ZRSDEMO_HTML_VIEWER and just added the below code for print functionality.

Please go through the code and try to adopt your solution from it. I issue i see in your code is in show_url method. Check the same in the above demo program.

Also i don't think you should pass the protocol file:\\ as it just accepts only the file path. Still if you want to use it you should give it as "file:\\\".

Hope this helps.

Regards,

Karthik D

Read only

0 Likes
4,138

Hi Alex,

I just down-ported that program as per your requirement. Just create a program with a screen 100 which contains a custom control with name pdf_file and paste the below code. It will work like a Charm .

DATA url_doc TYPE char255.
DATA pdf_file TYPE REF TO cl_gui_html_viewer.
DATA pdf_container TYPE REF TO cl_gui_custom_container.
DATA OK_CODE like SY-UCOMM.
SET SCREEN 100.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module STATUS_0100 output.
   SET PF-STATUS space.
*  SET TITLEBAR 'xxx'.
   IF pdf_container is initial.
     CREATE OBJECT pdf_container
             EXPORTING container_name = 'PDF_FILE'.
     CREATE OBJECT pdf_file
             EXPORTING  parent              = pdf_container
             EXCEPTIONS cntl_error         = 1
                        cntl_install_error = 2
                        dp_install_error   = 3
                        dp_error           = 4.
     IF sy-subrc ne 0.
* Fehlerbehandlung
     ELSE.
*CONCATENATE 'file://' v_file INTO url_doc.
url_doc = 'd:/sapnote_0001631737.pdf'.

CALL METHOD pdf_file->show_url
    EXPORTING
      url = url_doc.



     CALL METHOD cl_gui_cfw=>flush
          EXCEPTIONS cntl_system_error = 1
                     cntl_error        = 2.
     IF sy-subrc ne 0.
* Fehlerbehandlung
     ENDIF.
*   Flush
     CALL METHOD cl_gui_cfw=>flush.
*   print
     CALL METHOD pdf_file->execwb
        EXPORTING
          cmd_id     = pdf_file->wb_cmdid_print
        EXCEPTIONS
          cntl_error = 1.

     IF sy-subrc <> 0.
        MESSAGE e003(cnht) RAISING html_print_error.
     ENDIF.
     ENDIF.
   ENDIF.
endmodule.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module USER_COMMAND_0100 input.
IF OK_CODE = 'BACK' OR OK_CODE ='%EX' OR OK_CODE ='RW'.
   CALL METHOD pdf_file->free.
   CALL METHOD pdf_container->free.
   FREE pdf_file.
   FREE pdf_container.
   LEAVE PROGRAM.
ENDIF.
endmodule.                 " USER_COMMAND_0100  INPUT

Regards,

Karthik D

Read only

0 Likes
4,138

Wow, Thank you very much. I was trying the same very thing without luck yet Save my life XD

Thanks again,

Alexandru F