You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Luke Shannon <ls...@futurebrand.com> on 2005/01/05 22:58:29 UTC

Need Some Help

Hi All;

Trying to get a FOP demo off the ground. The idea is an FO document comes in
from memory and is rendered to a PDF on the hard disk of the server.

Just to get things going I am working with a FO file on the hard drive as
well. I will tackle the contents of this file submitted in memory next.

Below is my code.

When I run it in IE it get a Pop up message telling me the "File does not
begin with '%PDF-'". This is weird to me because I am not trying to send it
back through the response object, I don't know why the browser is even
looking at it.

When I run it in FireFox I get my success message printed, and the document
created. However when I trying and open it I am told there is a Sharing
Violation. The writer was closed before I printed the success statement so I
am not sure where the conflict came from.

Is there a better, smarter or easier way to do this? Any help would be
appreciated.

Thanks,

Luke

/*
 * Test class to get this FOP stuff working
 */

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.fop.apps.Driver;
import org.xml.sax.*;

/**
 * This class is intended to demonstrate submitting a FO document in memory
 * (String) to FOP and write a PDF document to the server.
 */
public class FopServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse
response)
    throws ServletException {
        try {
            //the input
            File fofile = new
File("D:\\applications\\fop\\fop-0.20.5\\test.fo");
            //location to write to
            File indexPDF = new
File("D:\\applications\\fop\\fop-0.20.5\\index.pdf");
            //create an input stream from the input file
            InputStream in = new FileInputStream(fofile);
            //FOP Driver
            Driver driver = new Driver();
            //set the render type
            driver.setRenderer(Driver.RENDER_PDF);
            //Setup output stream
            OutputStream out = new FileOutputStream(indexPDF);
            out = new BufferedOutputStream(out);
            //configure the input and the output
            driver.setInputSource(new
InputSource("D:\\applications\\fop\\fop-0.20.5\\test.fo"));
            driver.setOutputStream(out);
            //run the driver
            driver.run();
            //get the content
            String pdfFileContent = out.toString();
            //create the file writer
            FileWriter writer = new FileWriter(indexPDF);
            //get content into buffer
            BufferedReader br = new BufferedReader(new
StringReader(pdfFileContent));
            //inialize a string to hold each line
            String str = "";
            //write the file
            while ((str = br.readLine()) != null) {
                writer.write(str);
            }
            //close up the writer
            writer.close();
            //write the success message
            PrintWriter output = response.getWriter();
            output.println("<html><head><title>Success</title></head>\n"
            + "<body><h1>Nice Work!</h1></body></html>");
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by tts 22 <tt...@hotmail.co.jp>.
I set "file:///C:\fop-0.20.5\msmincho.xml" in userconfig.xml.
And this made creation of PDF in shift_jis through servlet, which
in fact, I had given up making one.(it is one of FAQ?!!) 

you helped me great, thank you.

tts6 

>From: Jeremias Maerki <de...@greenmail.ch>
>Reply-To: fop-user@xml.apache.org
>To: fop-user@xml.apache.org
>Subject: Re: Need Some Help
>Date: Fri, 07 Jan 2005 07:57:26 +0100
>
>Are you using relative URLs in userconfig.xml? If yes, you need to set
>the baseDir/baseURL (or fontBaseDir, if it is different from baseDir)
>accordingly. The other possibility is to use absolute paths or URLs.
>
>http://xml.apache.org/fop/faq.html#fonts-not-found
>http://xml.apache.org/fop/embedding.html#config-internal
>http://xml.apache.org/fop/configuration.html#summary-key-value
>
>I answered something similar just two days ago:
>http://mail-archives.apache.org/eyebrowse/ReadMsg?listName=fop-user@xml.apache.org&msgNo=12960

>
>If that doesn't help please show us your userconfig.xml.
>
>On 07.01.2005 06:33:10 tts 22 wrote:
> > Following your instruction, builded successfully.
> >
> > but Tomcat's errors.
> > [ERROR] Logger not set
> > [ERROR] Failted to read a font metrics file: Invalid font metrics file:
> > msmincho.xml (no protpcol: msmincho.xml)
> > [ERROR] unknown font Mincho, so defaulted font to any
> >
> > Can I  have any hint?
>
>
>Jeremias Maerki
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
>For additional commands, e-mail: fop-user-help@xml.apache.org
>

_________________________________________________________________
日本国内最大級570万ユーザーのMSN Hotmail 
https://registernet.passport.net/reg.srf?id=2&lc=1041 


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by Jeremias Maerki <de...@greenmail.ch>.
Are you using relative URLs in userconfig.xml? If yes, you need to set
the baseDir/baseURL (or fontBaseDir, if it is different from baseDir)
accordingly. The other possibility is to use absolute paths or URLs.

http://xml.apache.org/fop/faq.html#fonts-not-found
http://xml.apache.org/fop/embedding.html#config-internal
http://xml.apache.org/fop/configuration.html#summary-key-value

I answered something similar just two days ago:
http://mail-archives.apache.org/eyebrowse/ReadMsg?listName=fop-user@xml.apache.org&msgNo=12960

If that doesn't help please show us your userconfig.xml.

On 07.01.2005 06:33:10 tts 22 wrote:
> Following your instruction, builded successfully.
> 
> but Tomcat's errors.
> [ERROR] Logger not set
> [ERROR] Failted to read a font metrics file: Invalid font metrics file: 
> msmincho.xml (no protpcol: msmincho.xml)
> [ERROR] unknown font Mincho, so defaulted font to any
> 
> Can I  have any hint?


Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by tts 22 <tt...@hotmail.co.jp>.
Hi

Following your instruction, builded successfully.

but Tomcat's errors.
[ERROR] Logger not set
[ERROR] Failted to read a font metrics file: Invalid font metrics file: 
msmincho.xml (no protpcol: msmincho.xml)
[ERROR] unknown font Mincho, so defaulted font to any

Can I  have any hint?

>From: Jeremias Maerki <de...@greenmail.ch>
>Reply-To: fop-user@xml.apache.org
>To: fop-user@xml.apache.org
>Subject: Re: Need Some Help
>Date: Thu, 06 Jan 2005 15:13:04 +0100
>
>If you mean:
>
>File userConfigFile = new File("C:\\fop\\conf\\userconfig.xml");
>new Options(userConfigFile);
>
>...then, yes.
>
>On 06.01.2005 14:58:12 tts 22 wrote:
> > File userConfigFile = new File("C:\\fop\\conf\\userconfig.xml");
> >
> > is this ok?
>
>
>Jeremias Maerki
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
>For additional commands, e-mail: fop-user-help@xml.apache.org
>

_________________________________________________________________
無料250MBメールボックスのMSN Hotmail 
https://registernet.passport.net/reg.srf?id=2&lc=1041 


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by Jeremias Maerki <de...@greenmail.ch>.
If you mean: 

File userConfigFile = new File("C:\\fop\\conf\\userconfig.xml");
new Options(userConfigFile);

...then, yes.

On 06.01.2005 14:58:12 tts 22 wrote:
> File userConfigFile = new File("C:\\fop\\conf\\userconfig.xml");
> 
> is this ok?


Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by tts 22 <tt...@hotmail.co.jp>.
thanks

File userConfigFile = new File("C:\\fop\\conf\\userconfig.xml");

is this ok?

tts6

>From: Jeremias Maerki <de...@greenmail.ch>
>Reply-To: fop-user@xml.apache.org
>To: fop-user@xml.apache.org
>Subject: Re: Need Some Help
>Date: Thu, 06 Jan 2005 13:13:57 +0100
>
>Sure, please see here: 
http://xml.apache.org/fop/embedding.html#config-external
>It describes how to use the userconfig.xml file from Java.
>
>On 06.01.2005 13:02:20 tts 22 wrote:
> > Can Font Metric msmincho.xml  be refered in this Luke's FopServlet?
>
>
>Jeremias Maerki
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
>For additional commands, e-mail: fop-user-help@xml.apache.org
>

_________________________________________________________________
MSN Hotmailで開運メルアド占い実施中 
http://promotion.msn.co.jp/hotmail/fortune/input_un.asp 


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by Jeremias Maerki <de...@greenmail.ch>.
Sure, please see here: http://xml.apache.org/fop/embedding.html#config-external
It describes how to use the userconfig.xml file from Java.

On 06.01.2005 13:02:20 tts 22 wrote:
> Can Font Metric msmincho.xml  be refered in this Luke's FopServlet?


Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by Luke Shannon <ls...@futurebrand.com>.
Responding to myself.

I have found the problem, this code below works. I am still interested if
anyone knows better way to do this.

Thanks,

Luke


/*
 * Test class to get this FOP stuff working
 */

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.fop.apps.Driver;
import org.xml.sax.*;

/**
 * This class is intended to demonstrate submitting a FO document in memory
 * (String) to FOP and write a PDF document to the server.
 */
public class FopServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse
response)
    throws ServletException {
        try {
            //the input
            File fofile = new
File("D:\\applications\\fop\\fop-0.20.5\\test.fo");
            //location to write to
            File indexPDF = new
File("D:\\applications\\fop\\fop-0.20.5\\index.pdf");
            //create an input stream from the input file
            InputStream in = new FileInputStream(fofile);
            //FOP Driver
            Driver driver = new Driver();
            //set the render type
            driver.setRenderer(Driver.RENDER_PDF);
            //Setup output stream
            OutputStream out = new FileOutputStream(indexPDF);
            out = new BufferedOutputStream(out);
            //configure the input and the output
            driver.setInputSource(new
InputSource("D:\\applications\\fop\\fop-0.20.5\\test.fo"));
            driver.setOutputStream(out);
            //run the driver
            driver.run();
            out.close();
            PrintWriter output = response.getWriter();
            output.println("<html><head><title>Success</title></head>\n"
            + "<body><h1>Nice Work!</h1></body></html>");
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }
}
----- Original Message ----- 
From: "Luke Shannon" <ls...@futurebrand.com>
To: <fo...@xml.apache.org>
Sent: Wednesday, January 05, 2005 4:58 PM
Subject: Need Some Help


> Hi All;
>
> Trying to get a FOP demo off the ground. The idea is an FO document comes
in
> from memory and is rendered to a PDF on the hard disk of the server.
>
> Just to get things going I am working with a FO file on the hard drive as
> well. I will tackle the contents of this file submitted in memory next.
>
> Below is my code.
>
> When I run it in IE it get a Pop up message telling me the "File does not
> begin with '%PDF-'". This is weird to me because I am not trying to send
it
> back through the response object, I don't know why the browser is even
> looking at it.
>
> When I run it in FireFox I get my success message printed, and the
document
> created. However when I trying and open it I am told there is a Sharing
> Violation. The writer was closed before I printed the success statement so
I
> am not sure where the conflict came from.
>
> Is there a better, smarter or easier way to do this? Any help would be
> appreciated.
>
> Thanks,
>
> Luke
>
> /*
>  * Test class to get this FOP stuff working
>  */
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import org.apache.fop.apps.Driver;
> import org.xml.sax.*;
>
> /**
>  * This class is intended to demonstrate submitting a FO document in
memory
>  * (String) to FOP and write a PDF document to the server.
>  */
> public class FopServlet extends HttpServlet {
>
>     public void doGet(HttpServletRequest request, HttpServletResponse
> response)
>     throws ServletException {
>         try {
>             //the input
>             File fofile = new
> File("D:\\applications\\fop\\fop-0.20.5\\test.fo");
>             //location to write to
>             File indexPDF = new
> File("D:\\applications\\fop\\fop-0.20.5\\index.pdf");
>             //create an input stream from the input file
>             InputStream in = new FileInputStream(fofile);
>             //FOP Driver
>             Driver driver = new Driver();
>             //set the render type
>             driver.setRenderer(Driver.RENDER_PDF);
>             //Setup output stream
>             OutputStream out = new FileOutputStream(indexPDF);
>             out = new BufferedOutputStream(out);
>             //configure the input and the output
>             driver.setInputSource(new
> InputSource("D:\\applications\\fop\\fop-0.20.5\\test.fo"));
>             driver.setOutputStream(out);
>             //run the driver
>             driver.run();
>             //get the content
>             String pdfFileContent = out.toString();
>             //create the file writer
>             FileWriter writer = new FileWriter(indexPDF);
>             //get content into buffer
>             BufferedReader br = new BufferedReader(new
> StringReader(pdfFileContent));
>             //inialize a string to hold each line
>             String str = "";
>             //write the file
>             while ((str = br.readLine()) != null) {
>                 writer.write(str);
>             }
>             //close up the writer
>             writer.close();
>             //write the success message
>             PrintWriter output = response.getWriter();
>             output.println("<html><head><title>Success</title></head>\n"
>             + "<body><h1>Nice Work!</h1></body></html>");
>         } catch (Exception ex) {
>             throw new ServletException(ex);
>         }
>     }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: fop-user-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by Luke Shannon <ls...@futurebrand.com>.
Thank you Andreas! Works very well.

----- Original Message ----- 
From: "Andreas L. Delmelle" <a_...@pandora.be>
To: <fo...@xml.apache.org>
Sent: Wednesday, January 05, 2005 6:21 PM
Subject: RE: Need Some Help


> > -----Original Message-----
> > From: Luke Shannon [mailto:lshannon@futurebrand.com]
> >
>
> Hi,
>
> > My new issue is how to render the PDF with a string input containing the
> > contents of the FO file.
>
> From java.lang.String to java.io.StringReader to java.io.Reader to
> org.xml.sax.InputSource?
>
> HTH!
>
> Greetz,
>
> Andreas
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: fop-user-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


RE: Need Some Help

Posted by "Andreas L. Delmelle" <a_...@pandora.be>.
> -----Original Message-----
> From: Luke Shannon [mailto:lshannon@futurebrand.com]
>

Hi,

> My new issue is how to render the PDF with a string input containing the
> contents of the FO file.

>From java.lang.String to java.io.StringReader to java.io.Reader to
org.xml.sax.InputSource?

HTH!

Greetz,

Andreas


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by Luke Shannon <ls...@futurebrand.com>.
Hi;

Thanks for the response. I fixed the problem. Had the OutputStream still
opened. I responded to my previous question myself with the new functioning
code.

I guess the forum (or my mail server) is slow.

My new issue is how to render the PDF with a string input containing the
contents of the FO file.

Luke

----- Original Message ----- 
From: "J.Pietschmann" <j3...@yahoo.de>
To: <fo...@xml.apache.org>
Sent: Wednesday, January 05, 2005 6:01 PM
Subject: Re: Need Some Help


> Luke Shannon wrote:
> > When I run it in IE
> Ouch! :-)
>
> > it get a Pop up message telling me the "File does not
> > begin with '%PDF-'". This is weird to me because I am not trying to send
it
> > back through the response object, I don't know why the browser is even
> > looking at it.
>
> Does the URL which invokes the servlet end in .pdf? IEx is easily
> fooled, especially in absence of a content-type header.
>
> Try to use an URL which ends in .htm or .html, or explicitely set
> the content type to text/html, or both.
>
> J.Pietschmann
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: fop-user-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Need Some Help

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Luke Shannon wrote:
> When I run it in IE
Ouch! :-)

> it get a Pop up message telling me the "File does not
> begin with '%PDF-'". This is weird to me because I am not trying to send it
> back through the response object, I don't know why the browser is even
> looking at it.

Does the URL which invokes the servlet end in .pdf? IEx is easily
fooled, especially in absence of a content-type header.

Try to use an URL which ends in .htm or .html, or explicitely set
the content type to text/html, or both.

J.Pietschmann


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


RE: Need Some Help

Posted by "Andreas L. Delmelle" <a_...@pandora.be>.
> -----Original Message-----
> From: Luke Shannon [mailto:lshannon@futurebrand.com]
>

Hi,

I see you've already discovered it yourself, but here's a bit more info on
why I think it went wrong...

> Below is my code.
>
> When I run it in IE it get a Pop up message telling me the "File does not
> begin with '%PDF-'". This is weird to me because I am not trying
> to send it back through the response object, I don't know why the browser
is even
> looking at it.
>

It may be weird, but it does point out one serious mistake:

>             //get the content
>             String pdfFileContent = out.toString();

PDF is not 'just text', so it's wrong to use a String to represent it. That
is not to say a PDF can't be opened in a text-editor --it can, but changing
a character and saving can seriously muck up the binary portions of the
file... not to mention prepend it with UTF-garbage.
The error message is Acrobat's IE-plugin complaining, and refers to the PDF
specification. A PDF file is supposed to start with:
%PDF-{version-number}
followed by a few (three or four... will have to look that up :-)) non-text
bytes.
These latter bytes will become 'characters' when you pass them through a
String, so the end-result is likely a corrupt PDF file, even if you're not
trying to display it.

> When I run it in FireFox I get my success message printed, and
> the document created. However when I trying and open it I am
> told there is a Sharing Violation.
> The writer was closed before I printed the success statement so I
> am not sure where the conflict came from.

Well... I don't fully agree. Keep in mind that in your initial code, the
physical PDF-file was referenced (for read/write access) by three different
objects: one File, and an OutputStream and a FileWriter wrapped around that
File. Only the latter of the three explicitly closed, which may have caused
the conflict to occur.
Open a PDF for writing in Acrobat, ask FOP to render that PDF and you'll
receive an error. Open it in Adobe Reader, ask FOP again, and it might work,
since the first handle on the file is read-only. (Since I tried the first
only on Windows, the latter only on Mac, this phenomenon could as well be
OS-dependent.)


Greetz,

Andreas


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org