<<

Contents

Search

>>

 

PDF Files

 

Java has no classes for creating PDF files.

 

With a PDF printer (e.g. Bullzip PDF Printer under Windows or Cups-PDF under Linux) PDF files can be created. By the help of freeware programs this is possible, too. Below you'll find an example how to create PDF files with the freeware program wkhtmltopdf.

 

wkhtmltopdf runs under Windows and Linux, can be called in command line mode and converts Html files to PDF files.

 

By the help of the function LPRINT_TO_HTML a Html file can be created, which can be converted into a PDF file by wkhtmltopdf.

 

The following example supports standard Java programs, 2T programs and Html programs.

The directory which contains wkhtmltopdf (resp. wkhtmltopdf-i386 with Linux) has to be added to the environment variable PATH.

ebusprint.js has to be in the directory of the Ebus-2T-Server.

 

Example:

 

PRINTER = "" // Start print job

...

REPORT #100

 

HTML_FILE$ = "invoice_" + STRN$(NUMBER) + ".html" // name of Html file

PDF_FILE$ = "invoice_" + STRN$(NUMBER) + ".pdf"   // name of PDF file

 

H% = LPRINT_TO_HTML(HTML_FILE$) // close print job and create Html file

                                // Attention: With Html and 2T programs the Html file is stored in the directory of the Ebus-2T-Server.

IF H% <> 0

THEN ERRORMES = "Error " + STR$(H%)

     QUIT

ENDIF

 

OS_SERVER$ = LOWER$(GET_SERVER_PROPERTY$("os.name")) // server operating system

OS_CLIENT$ = LOWER$(GET_CLIENT_PROPERTY$("os.name")) // client operating system

 

PDF_HAS_BEEN_CREATED% = 0

H_STDOUT$ = ""

 

IF INSTR(OS_SERVER$,"windows") > 0 // Windows

THEN COMMAND$ = "cmd /c wkhtmltopdf --disable-smart-shrinking " + HTML_FILE$ + " " + PDF_FILE$

ELSE IF INSTR(OS_SERVER$,"linux") > 0 // Linux

     THEN COMMAND$ = "wkhtmltopdf-i386 --disable-smart-shrinking " + HTML_FILE$ + " " + PDF_FILE$

     ELSE ERRORMES = "This operating system is not supported: " + OS_SERVER$

          QUIT

     ENDIF

ENDIF

 

ENABLE WAITSHELL

SHELL(SHELL.STDOUT) COMMAND$

 

IF PDF_HAS_BEEN_CREATED% = 0

THEN ERRORMES = "Error: " + H_STDOUT$ // Display standard output

     QUIT

ENDIF

 

#IF GEN_HTML // Html program

// Show PDF file in a browser window

H% = SHOW_DOCUMENT(PDF_FILE$,"_blank",1)

IF H% <> 0

THEN ERRORMES = "Error " + STR$(H%)

ENDIF

#ELSE

  #IF GEN_JAVA_2T // Java 2T program

        // copy PDF file to client

        H% = COPY_FILE_TO_CLIENT(PDF_FILE$,PDF_FILE$)

        IF H% <> 0

        THEN ERRORMES = "Error: " + ERRORTEXT$

             QUIT

        ENDIF

 

        // show PDF file

        IF INSTR(OS_CLIENT$,"windows") > 0

        THEN SHELL_CLIENT "cmd /c " + PDF_FILE$

        ENDIF

 

        IF INSTR(OS_CLIENT$,"linux") > 0

        THEN SHELL_CLIENT "okular " + PDF_FILE$ // Okular is available under Linux KDE.

                                                // You can use other programs (e.g. gimp) to show the PDF file.

        ENDIF

  #ELSE // standard Java program

 

        // show PDF file

        IF INSTR(OS_CLIENT$,"windows") > 0

        THEN SHELL "cmd /c " + PDF_FILE$

        ENDIF

 

        IF INSTR(OS_CLIENT$,"linux") > 0

        THEN SHELL "okular " + PDF_FILE$

        ENDIF

  #ENDIF

#ENDIF

 

 

Macro SHELL.STDOUT:

 

H_STDOUT$ = H_STDOUT$ + EBUS_LINEFEED$ + EBUS_STDOUT$

 

IF INSTR(LOWER$(EBUS_STDOUT$),"done") > 0 // wkhtmltopdf reports "Done" if the PDF file has been created.

THEN PDF_HAS_BEEN_CREATED% = 1

ENDIF