pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Guy Kroizman" <Guy.Kroiz...@ngsoft.com>
Subject RE: hello word example generated un-openable file.
Date Sun, 08 Jul 2012 12:40:36 GMT
Hi, 
Yes, indeed on a regular java program it works fine. I guess then the problem is not pdfbox
related.
Thanks.




Guy Kroizman
Sofware Engineer
Omer Industrial Park, Israel | Tel: +972 (0) 8 6262329 | Fax: +972 (0) 8 6262201 | Mobile:
+972 (52) 8834115
www.ngsoft.com




-----Original Message-----
From: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
Sent: Sunday, July 08, 2012 12:59 PM
To: users@pdfbox.apache.org
Subject: Re: hello word example generated un-openable file.

Hi Guy,

your code works fine for me. The only change I did - as I didn't want to deal with a servlet
- was that I run it as a cli test writing to a permanent file. Maybe you can test that first.
If that works how do you send the generated pdf back to the web application? Maybe the error
lies in there.

With kind regards

Maruan Sahyoun

Am 07.07.2012 um 10:39 schrieb Guy Kroizman:

> Hi
> I have a servlet which execute the hello word example but when I try to open the generated
pdf I get message from adobe reader:
> 
> "Adobe Reader could not open 'test.pdf' because it is either not a supported file type
or because the file has been damaged (for example, it was sent as an email attachment and
wasn't correctly decoded)."
> 
> Here is my code: 
> 
> PDDocument document = new PDDocument();
> PDPage page = new PDPage();
> document.addPage( page );
> PDFont font = PDType1Font.HELVETICA_BOLD;
> PDPageContentStream contentStream = new PDPageContentStream(document, page);
> contentStream.beginText();
> contentStream.setFont( font, 12 );
> contentStream.moveTextPositionByAmount( 100, 700 );
> contentStream.drawString( "Hello World" );
> contentStream.endText();
> contentStream.close();
> 
> // get the web applications temporary directory
> File tempDir = (File) getServletContext().getAttribute( "javax.servlet.context.tempdir"
);
> 
> // create a temporary file in that directory
> File tempFile = File.createTempFile( "test", ".pdf", tempDir );
> 
> 
> try {
>            document.save(tempFile.getAbsolutePath());
> } catch (COSVisitorException e) {
>            e.printStackTrace();
> }
> document.close();
> 
> 
> Thanks.
> Guy Kroizman.

Mime
View raw message