Re: send to local printer

Yuri Singidas <[email protected]>
Newsgroups gmane.comp.java.sun.servlet
Organization OIT
Message-ID <[email protected]>
Aside from sending automatically the pdf files directly to the printer
without invoking the Acrobat dialog box, I also create hyperlinks to
each of the pdf file generated so that the user can open each in the
browser and print it in case the previous process fails.

It would be nice if i can allow the user to generate multiple
pdfs(actually, student certificates) and automatically print them from
his local printer.

here's my code to print it automatically after generating the pdf:

public boolean uploadFile(String directory, String filename, String
student, String school){

        boolean filecreated = false;

        String myfile = filename.trim();
        String mystudent = student.trim();
        String myschool = school.trim();
        String mydirectory = directory.trim();
        int charsize = mystudent.length();
        int charsize2 = myschool.length();
        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4.rotate());

        try {

            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(myfile));


            // we add a Watermark that will show up
            try {
                Watermark watermark = new
Watermark(Image.getInstance(mydirectory), 50, 20);
                document.add(watermark);
            }
            catch(Exception e) {
                System.err.println("watermark image in wrong path..");
            }

            // step 3: we open the document
            document.open();

            // step 4: we grab the ContentByte and do some stuff with it
            PdfContentByte cb = writer.getDirectContent();


            // we tell the ContentByte we're ready to draw text
            cb.beginText();

            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.setFontAndSize(bf, 14);

            // we draw some text on a certain position
            int xpos = 400 - (charsize*3);
            cb.setTextMatrix(xpos, 316);
            cb.showText(mystudent);
            System.out.println("xpos="+xpos);

            int xpos2 = 400 - (charsize2*3);
            cb.setTextMatrix(xpos2, 270);
            cb.showText(myschool);
            System.out.println("xpos2="+xpos2);

            // we tell the contentByte, we've finished drawing text
            cb.endText();
            filecreated = true;
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
            filecreated = false;
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
            filecreated = false;
        }

        // step 5: we close the document
        document.close();


        return filecreated;
    }

    public boolean printFile(String filename){
        boolean fileprinted = false;

        String myfile = filename.trim();

        try{

              String osName = System.getProperty("os.name" );
              //FOR WINDOWS 95 AND 98 USE COMMAND.COM
              if( osName.equals( "Windows 95" ) || osName.equals(
"Windows 98" )){
                  Runtime.getRuntime().exec("command.com /C start
acrord32 /p /h" + myfile);
                  System.out.println("printing pdf..95.."+ myfile);
              }
              //FOR WINDOWS NT/XP/2000 USE CMD.EXE
              else {
                  Runtime.getRuntime().exec("cmd.exe /C start acrord32
/p /h" + myfile);
                  System.out.println("printing pdf..2000.."+ myfile);
              }
              fileprinted = true;
          }
          catch(IOException ioe) {
                System.err.println(ioe.getMessage());
                fileprinted = false;
          }


        return fileprinted;

Rich Shonk wrote:

>
> Since you are attempting to access a local printer, the printing needs
> to be handled on the user machine.
> You will need to send the pdf to the user and print from Acrobat Reader.
>
> Rich
>
> > local printers attached to a user machine
> >
> >
> > Yuri,
> >
> > > My problem is: once my app is deployed  in the unix box
> > > (test/production servers), i can generate the pdf files and store
> them
> > > in the server but i won't be able to execute the runtime command to
> > send
> > > the files to the local machine's printer.
> >
> > Are you attempting to print to network printers accessible by IP
> > address or local printers attached to a user machine?
> >
> > Rich

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.