Baanboard.com

Baanboard.com (http://www.baanboard.com/baanboard/index.php)
-   Code & Utilities (http://www.baanboard.com/baanboard/forumdisplay.php?f=33)
-   -   DEV: Create PDF using Java iText (http://www.baanboard.com/baanboard/showthread.php?t=14591)

ahulikavi 25th February 2004 05:05

Another Option
 
We have developed baan to pdf functionality using Java iText Class, Java class is executed through shell script which is called by 4GL baan script.

More information at
http://www.lowagie.com/iText/

Following things are done presently :

Any baan report is converted into PDF, page size/Font is chosen automatically based on report size.

PDF is encrypted and only Print choice is enabled.

Company Logo and Custom footer with Date/Time Stamp and User details added.

Things not done :

Font effects of report not carried over since basically its a text to PDF converter. (How to get formatting details ? (report XML could help ???)

~Vamsi 25th February 2004 05:36

Akshay,

Is the code you developed open source? If it is please either post it to this forum or a link to some other location.

If it is not open source... I will move the announcement to a different forum.

ahulikavi 25th February 2004 07:14

Java Class based on i-Text
 
Hi,

3 components are required

1. Baan 3GL to convert report to ASCII and invoke shell scripts
2. Java Class to convert text to pdf
3. Shell scripts to invoke java class
4. Device Definition

1. Baan 3GL - Hope copyright is OK

Code:

||******************************************************************************
|* tdtcdpdf        0  VRC B40C L ive               
|* Title        : PDF
|* Author        : Akshay Hulikavi
|* Date                : 2004-02-09 [09:17]
|******************************************************************************
|* License        :
|* Copyright 2004 by Akshay Hulikavi
|*
|* All Rights Reserved
|*
|* Permission to use, copy, modify, and distribute this software and its
|* documentation for any purpose and without fee is hereby granted,
|* provided that the above copyright notice appear in all copies and that
|* both that copyright notice and this permission notice appear in
|* supporting documentation.
|*
|* Akshay Hulikavi DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
|* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|* AND FITNESS, IN NO EVENT SHALL Akshay Hulikavi BE LIABLE FOR ANY
|* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
|* OR PERFORMANCE OF THIS SOFTWARE.
|******************************************************************************
        #pragma used dll ottdllbw
       
        long                ret, fp, zoomed        | general return variable
        extern        domain        tcmcs.str215        input.file, pdf.file, local.file        | input file - created by Baan
        extern        domain        tcmcs.str215        argument, argu1, argu4, argu0, argu2, argu3
        extern        domain        tcmcs.str215        argu5, argu6, argu7, argu8, argu9
        extern string        sh.string(1024), file.path(128), date.str(8), time.str(5), dat(12)

function main()
{
        argu0 = argv$(0)
        argu1 = argv$(1)
        argu2 = argv$(2)
        argu3 = argv$(3)
        argu4 = argv$(4)
        zoomed = false

        dat = dte$()
        date.str = dat(3;2) & "/" & dat(1;2)& "/" & dat(5;2)
        time.str = dat(7;2) & ":" & dat(9;2)
       
        if argu1 = "" and argu2= "" then        | Called from another program not spooler
            import("argu1", argu1)
            import("argu2", argu2)
            import("argu3", argu3)
            import("argu4", argu4)
            zoomed = true
        endif   
           
        file.path = getcwd()
        input.file = shiftl$(strip$(argu2))
        ret = wait.and.activate("ttstpconv", argu1, input.file, "ASCII", "wt")


        input.file = shiftl$(strip$(file.path))&"/"&shiftl$(strip$(input.file))
        pdf.file = input.file&".pdf"
        local.file = "c:\"&shiftl$(strip$(argu2))&".pdf"

        sh.string = ""
        sh.string = sh.string &" "&chr$(34)&input.file&chr$(34)
        sh.string = sh.string &" "&chr$(34)&pdf.file&chr$(34)
        sh.string = sh.string &" "&str$(spool.pg.length)
        sh.string = sh.string &" "&chr$(34)&logname$&" on "& date.str & " at "& time.str& chr$(34)

        fp=seq.open("text2pdf.sh","r")       
        if fp >= 1 then | text2pdf.sh file exists
                ret = shell( "text2pdf.sh " & sh.string,0)
        else       
                fp=seq.open("text2pdf.sh","wt")       
                seq.puts("cd /baanIVc/c4/bse/tcd_email", fp)
                seq.puts("pdf.sh $@", fp)
                seq.close(fp)
                ret = shell( "chmod 777 text2pdf.sh",0)
                ret = shell( "text2pdf.sh " & sh.string,0)
        endif       

        if ret = 0 then
                if zoomed = false then        | Called from another program not report
                        ret =  server2client(pdf.file,local.file,  0, 0)  | Binary Mode Transfer
                        if ret = 0 then
                                ret =  app_start(local.file,"","","","")
                                ret = seq.unlink(input.file)
                                ret = seq.unlink(pdf.file)
                        else       
                                message("File could sent to client !!!!" & str$(ret)&local.file)
                        endif       
                endif       
        else
                message("File could not be created !!!!" & str$(ret))
        endif       

}
|*********************************** end of script ****************************




2. the java class tcd_text2pdf
Code:


/*
Class to convert baan ascii report to pdf
Author : Akshay Hulikavi
Date    : 10.02.2004

Requires : itext-x.jar - can be downloaded from <a href="http://www.lowagie.com/iText/" target="_blank">www.lowagie.com/iText/</a>
Source can be downloaded from <a href="http://www.lowagie.com/iText/" target="_blank">www.lowagie.com/iText/</a>

Usage : java tcd_text2pdf  <inputfile> <outputfile.pdf> <PageLength, Default = 60>

*/

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;

public class tcd_text2pdf
{

public static void main(String[] args)
        throws IOException, DocumentException
        {
        String resultStr = "", line=null, strFooter;
        int lncnt=1, i, j, pgcnt=0, fntSize=8, lineSpacing, strLength=0;       

        //~ i = Integer.parseInt(args[2]);
        j = Integer.valueOf(args[2]).intValue();
                if (j <= 0)
                        j = 60;  // Default page length

                if(args.length < 2 ) {
                        System.out.println("Usage : java tcd_text2pdf  <inputfile> <outputfile.pdf> <PageLength, Default = 60>");
                        return;       
                }       
       
        Reader in = new InputStreamReader(new FileInputStream(args[0]));
        OutputStream os = new FileOutputStream(args[1]);
        Document doc= new Document();
        Image t_logo = Image.getInstance("logocolor.jpg");
        t_logo.scalePercent(75);  // scale image to 75%
        java.io.BufferedReader  br2  = new java.io.BufferedReader(in); 

        // get length of the longest line       
                try {
                    BufferedReader bufReader = new BufferedReader(new FileReader(args[0]));
                    while ((line=bufReader.readLine()) != null){
                                        if (strLength < line.length())
                                                strLength = line.length();
                                }
                    bufReader.close();
               
                } catch (IOException e) {
                    System.err.println(e);
                }



        line = null;               
                i=1;

/*
        Decide on Paper Size ,Orientation and Line Spacing based on max line length in report
       
*/

                if (strLength <= 108)
                        i = 1 ;       
                if (strLength > 108 && strLength <= 127)
                        i = 2 ;       
                if (strLength > 127 && strLength <= 165)
                        i = 3 ;       
                if (strLength > 165 && strLength <= 176)
                        i = 4 ;       
                if (strLength > 176 && strLength <= 239)
                        i = 5;       
                if (strLength > 239)
                        i = 6 ;       
                       
        switch (i) {
                        case 1:
                                doc = new Document(PageSize.A4, 36, 18, 72, 18);
                                fntSize = 8;
                                lineSpacing = fntSize;
                                t_logo.setAbsolutePosition(535, 770);  // image margin 60 so 595-60 and 842-60
                        break;               
                        case 2:
                                doc = new Document(PageSize.A4, 36, 18, 72, 18);
                                fntSize = 7;
                                lineSpacing = 9;
                                t_logo.setAbsolutePosition(535, 770); 
                        break;               
                        case 3:
                                doc = new Document(PageSize.A4.rotate(), 36, 18, 72, 18);
                                fntSize = 8;
                                lineSpacing = fntSize;
                                t_logo.setAbsolutePosition(770, 535); 
                        break;
                        case 4:
                                doc = new Document(PageSize.A4.rotate(), 36, 18, 72, 18);
                                fntSize = 7;
                                lineSpacing = 9;
                                t_logo.setAbsolutePosition(770, 535); 
                        break;
                        case 5:
                                doc = new Document(PageSize.LEDGER, 36, 18, 72, 18);
                                fntSize = 8;
                                lineSpacing = fntSize;
                                t_logo.setAbsolutePosition(1164, 732); 
                        break;
                        case 6:
                                doc = new Document(PageSize.LEDGER, 36, 18, 72, 18);
                                fntSize = 7;
                                lineSpacing = 9;
                                t_logo.setAbsolutePosition(1164, 732); 
                        break;
                        default:
                                doc = new Document(PageSize.A4, 36, 36, 72, 18);
                                fntSize = 8;
                                lineSpacing = fntSize;
                                t_logo.setAbsolutePosition(535, 770);  // image margin 85 so 595-85 and 842-85
                        break;               

                        }
       
       
        // Add footer
        strFooter  = "Thermax Limited Chemical Division : 97-E General Block MIDC Bhosari Pune 411026";
        strFooter +="\nPhone - 91-20-27120181, Fax - 91-20-27120206, URL - <a href="http://www.thermaxindia.com/chemical" target="_blank">www.thermaxindia.com/chemical</a>";
        strFooter +="\nGenerated by (BaaN Login) : ";
        strFooter +=args[3];
       
        HeaderFooter footer = new HeaderFooter(new Phrase(strFooter,FontFactory.getFont(FontFactory.COURIER, 7)), false);
       
        PdfWriter docWriter = null;
        docWriter = PdfWriter.getInstance(doc, os);
        doc.setFooter(footer);
        docWriter.setEncryption(PdfWriter.STRENGTH40BITS, null, null,  PdfWriter.AllowPrinting);
        doc.addTitle("BaaN Report");
        doc.addSubject("Report Generated from BaaN System");
        doc.addKeywords("Thermax Limited Chemical Division : 97-E General Block MIDC Bhosari Pune 411026");
               
        doc.open();
            doc.add(t_logo);
        Paragraph p;
        java.io.BufferedReader  br  = new java.io.BufferedReader(in); 

        try        {
                while ((line=br.readLine()) != null){
                        if (lncnt > j)
                        {       
                                doc.newPage();
                                lncnt = 0;
                        }       
                        p = new Paragraph(new Phrase(lineSpacing,line,FontFactory.getFont(FontFactory.COURIER, fntSize)));
                        doc.add(p);
                        lncnt++;       
                        }
                        br.close();
                } catch (IOException e) {
                        System.out.println("IO Exception on Buffered Read :" + e);
                };
       
        doc.close();
        os.close();
}

}



3. Shell Scripts
a. text2pdf.sh in user home directory
Code:

cd /baanIVc/c4/bse/tcd_email
pdf.sh $@

b. pdf.sh in say $BSE or other convinient directory
Code:

CLASSPATH=.:/baanIVc/c4/bse/tcd_email/PeBSClasses.zip:/baanIVc/c4/bse/tcd_email/itext-1.01.jar:/usr/j2se/javamail/javamail-1.3/mail.
export CLASSPATH
java tcd_text2pdf $@


4. Device Definition
Also need to define device called say PDF as follows

Device type: Rewrite File
4GL Program : tdtcdpdf
Argument : %s
Page Length : 72



regards

~Vamsi 25th February 2004 19:18

Akshay,

I split the posting into two threads so that there is more visibility. You get the advantage of 4066 views to start with ;).

NPRao 8th May 2004 00:42

Sean C. Sullivan works for our company and there was Portland Java User Group meeting here in April.

Here is more info -

Portland Java User Group Meeting Info

The topic was - Generating reports with JasperReports and there is a ppt on the site for reference.

Generating reports with JasperReports
JasperReports is an open source Java reporting tool. In this presentation, we will discuss the Jasper engine API, Jasper XML report templates, and Jasper datasources. We'll also look at JasperAssistant, a visual report designer tool.

sant123 10th December 2004 23:36

colors in PDF report
 
hi, AKshay,

I have implemented your idea, and it just works great!!

Now I want to go one step further and color the report, any ideas??? I looked at iText library there seems to be the chunk java api which lets us specify the color for the paragraph? I am palnning to try that.

Any thoughts.

Thanks
Sant

veyant 1st June 2005 07:36

can't create file
 
Hi ahulikavi ,

I tried to implement your idea. but i am facing problem in it.

as per your instructions, i have copied all 4 components and now when i give print on the new device(PDF), it gives me error "File can't be created!!! 127"

actually in debugging i found that while existing command

ret = shell("text2pdf.sh" & sh.string,0) it returns 127 value to ret.

however, i tried to run text2pdf file on command prompt directly and it gives following error.

pdf.sh[2]: CLASSPATH^M: this is not an identifier.

is java needed to be installed on the server. i have java1.4 installed on the server.

here is the code of pdf.sh which i modified.

CLASSPATH=/app/baan4c4/bse/tcd_email/PeBSClasses.zip:/app/baan4c4/bse/tcd_email/itext-1.3.jar:/usr/java14/bin
export CLASSPATH
java tcd_text2pdf $@


PeBSClasses.zip file is not available on the path =/app/baan4c4/bse/tcd_email/

actually this file doesn't exist. please advice why it is throwing 127 value in return.

thanks
Sandy

ahulikavi 2nd June 2005 05:17

Hi Sandy,

pdf.sh is a script to ensure that classpath is set correctly, it needs to be modified based on your setup and where you placed the itext-1.03.jar file, please modify it accordingly. You can ignore other jars like PeBS..., these are not required for pdf.

create pdf.sh in a common directory accessible by all users, with owner bsp. For our setup this is /tcd_email in bse path, change it as required by you.

You will have to make same change in script...
seq.puts("cd your_directory_path_where_pdf.sh_is_present", fp)

Run the shell script directly and check if CLASSPATH is set.

As far as text2pdf.sh is concerned, ensure, that it has 777 permission.

Lets see if it works now ....

veyant1 16th June 2005 05:48

problem in converting to pdf
 
hi,

i have modified the pdf.sh and txt2pdf.sh shell scripts accordingly. but it still gives me error "file can not be created!!!127".

However, i tried to run the text2pdf.sh at shell itself in AIX.

it activates pdf.sh and gives me error "vaja not found'.

however in pdf.sh on line 3 i am calling

java text2pdf $@

in AIX at /usr/java14/jre/bin , i have the java components available. i have declared these paths in CLASSPATH in pdf.sh.

here are the codes for pdf.sh and text2pdf.sh.


pdf.sh:
=======================

CLASSPATH = /app/baan4c4/bse/tcd_email/itext-1.3.jar:/usr/java14/jre/bin
export CLASSPATH
java tcd_text2pdf $@
=========================


txt2pdf,sh:
=======================================
cd /app/baan4c4/bse/tcd_email
pdf.sh $@
=======================================

please help as txt2pdf.sh is working fine but pdf.sh is not working fine.

however, need one more clarification.

there is a file tcd_text2pdf which is a java class should this file be with .class extension or not.

thanks
Sandeep Makan

bdittmar 1st July 2005 16:10

do all in converter !
 
1 Attachment(s)
Hello,
attached is an converter i use.
The shell script is built in converter and handles the java classes.
So you only need this converter and nothing else.

Regards

veyant1 9th July 2005 11:23

Class not found
 
Hi Akshay,

I have still not got the break-thru in implementation of this utility.
here is what i have done.

I created a new folder in $BSE with tcd_email name and placed text2pdf.sh,
pdf.sh, tcd_text2pdf class, i-text.jar and source code for i-text with all related class files in it.

I copied your script in baan and attached it with a device. now when i try to check the utility, i found that it is failing in conversion.

i have installed all source and related class files in the folder. but it gives error "file can't be created". it fails in execuing pdf.sh.

i checked it seprately on shell and found that while running tcd_text2pdf it gives error com/lowagie/text/ELEMENT java class not found.

however, i check the path and found that ELEment class is present in the folder.

any clue please. can you let me know some tip on it.

thanks
Sandeep Makan

bdittmar 14th July 2005 16:24

java classes dirs
 
Quote:

Originally Posted by veyant1
Hi Akshay,

I have still not got the break-thru in implementation of this utility.
here is what i have done.

I created a new folder in $BSE with tcd_email name and placed text2pdf.sh,
pdf.sh, tcd_text2pdf class, i-text.jar and source code for i-text with all related class files in it.

I copied your script in baan and attached it with a device. now when i try to check the utility, i found that it is failing in conversion.

i have installed all source and related class files in the folder. but it gives error "file can't be created". it fails in execuing pdf.sh.

i checked it seprately on shell and found that while running tcd_text2pdf it gives error com/lowagie/text/ELEMENT java class not found.

however, i check the path and found that ELEment class is present in the folder.

any clue please. can you let me know some tip on it.

thanks
Sandeep Makan

Hello,
is the CLASSPATH set to the lowagie dir ?
set the absolute path /..../bse/..../.... to this dir.
the subdir structure must be the lowagie structure :

"classpath dir"
|
|----ant
|----com-----------|lowagie----|---bc++++++subdirs and files
| |---text+++++subdirs and files
|----META-INF |---tools

Regards

manojsharma 23rd August 2005 06:28

Problem
 
Hi,

I am facing the same problem as Veyant is facing i.e. 127. Bernad, I have used your script also but its giving the same error.

Sant, how do you do that. Can anyone explain. Its urgent.

Thanx in advance

manojsharma 24th August 2005 05:56

Hi
 
Hi Vamsi,

seeking for help from your side

ahulikavi 24th August 2005 06:09

Hi,

Only things I can think of are accuracy of
1. CLASSPATH ( Should also include required itext JAR's)
2. File Permissions (Execute permissions on script files and classes)
3. Directory permissions
4. path_names as defined in your system ( also adjust accordingly in JAVA, BaaN Program script and OS Script files)

Hope this resolves your problem, since it is working at other places.


All times are GMT +2. The time now is 23:49.


vB.Sponsors
©2001-2017 - Baanboard.com - Baanforums.com