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.