You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@forrest.apache.org by Alan Wang <al...@gmail.com> on 2010/07/06 06:27:06 UTC

Convert xdoc document to PDF

Hi All,

I am writing a program similar to pdf output plugin to convert xdoc pages in
xml document ( xdoc similar to http://xmlgraphics.apache.org/fop/index.xml),
and would like to generate the pdf output using fop (0.95 on Mac os 10.5) as
shown in the following.  However I am getting the following errors from
console.

(Location of error unknown)org.apache.fop.fo.ValidationException:
Error(Unknown location): For fo:simple-page-master, fo:region-body must be
declared before fo:region-before.

1. Where can I find the xsl to convert from xdoc to fo. I find one from
apache forrest
distribution( apache-forrest-0.8/main/webapp/skins/common/xslt/fo/document-to-fo.xsl)?
Or Am I using the wrong one for converting from xdoc to fo?

2. Is anything wrong with my code?

Regards,
Alan



========================================================
public class fop {
 public static void main(String[] args) throws Exception {
     // configure fopFactory as desired
            FopFactory fopFactory = FopFactory.newInstance();
            fopFactory.setStrictValidation(false);
            fopFactory.setStrictUserConfigValidation(false);

            // configure foUserAgent as desired
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

            // Setup output
            OutputStream out = new java.io.FileOutputStream(new
File("mypdf.pdf"));
            out = new java.io.BufferedOutputStream(out);

            // Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent,
out);

            // Setup XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(new
File("/Downloads/apache-forrest-0.8/main/webapp/skins/common/xslt/fo/document-to-fo.xsl")));

            // Set the value of a <param> in the stylesheet
            transformer.setParameter("versionParam", "2.0");

            // Setup input for XSLT transformation
Source src = new StreamSource("http://xmlgraphics.apache.org/fop/index.xml
");

            // Resulting SAX events (the generated FO) must be piped through
to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            // Start XSLT transformation and FOP processing
            transformer.transform(src, res);

            out.close();
}
}

Re: Convert xdoc document to PDF

Posted by David Crossley <cr...@apache.org>.
Alan Wang wrote:
> 
> I am writing a program similar to pdf output plugin to convert xdoc pages in
> xml document ( xdoc similar to http://xmlgraphics.apache.org/fop/index.xml),

Your questions are appropriate for the Apache FOP mailling lists:
http://xmlgraphics.apache.org/fop/maillist.html

-David

Re: Convert xdoc document to PDF

Posted by Alan Wang <al...@gmail.com>.
Hi Brain and Ferdinand,

Thanks for the suggestions. I will try to go with second approach and make
some changes to the stylesheets and see if it works.

Cheers,
Alan

On Mon, Jul 5, 2010 at 10:30 PM, Ferdinand Soethe <sa...@soethe.net>wrote:

>  Hi Alan,
>
> as far as I remember the stylesheets  for pdf-generation have moved from
> core into the pdf-output-plugin.
>
> So you may be looking at outdated style sheets. Which might also explain
> the errors you are getting (some changes were required in the
> transformations to accomodate that last fop-update).
>
> Check out
> plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets for the
> current stylesheets (from head).
> After using Forrest with the pdf-plugin for the first time, you will also
> find the stylesheets in
> build/plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets.
>
> Yet I agree with Brian, these style sheets were designed to be used in a
> pipeline, so you may have to make some major adjustments.
>
> Regards,
> Ferdinand Soethe
>
> Am 06.07.10 06:27, schrieb Alan Wang:
>
>  Hi All,
>
>  I am writing a program similar to pdf output plugin to convert xdoc pages
> in xml document ( xdoc similar to
> http://xmlgraphics.apache.org/fop/index.xml), and would like to generate
> the pdf output using fop (0.95 on Mac os 10.5) as shown in the following.
>  However I am getting the following errors from console.
>
>  (Location of error unknown)org.apache.fop.fo.ValidationException:
> Error(Unknown location): For fo:simple-page-master, fo:region-body must be
> declared before fo:region-before.
>
>  1. Where can I find the xsl to convert from xdoc to fo. I find one from
> apache forrest
> distribution( apache-forrest-0.8/main/webapp/skins/common/xslt/fo/document-to-fo.xsl)?
> Or Am I using the wrong one for converting from xdoc to fo?
>
>  2. Is anything wrong with my code?
>
>  Regards,
> Alan
>
>
>
>  ========================================================
> public class fop {
>   public static void main(String[] args) throws Exception {
>      // configure fopFactory as desired
>             FopFactory fopFactory = FopFactory.newInstance();
>             fopFactory.setStrictValidation(false);
>             fopFactory.setStrictUserConfigValidation(false);
>
>             // configure foUserAgent as desired
>             FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
>
>              // Setup output
>             OutputStream out = new java.io.FileOutputStream(new
> File("mypdf.pdf"));
>             out = new java.io.BufferedOutputStream(out);
>
>              // Construct fop with desired output format
>             Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
> foUserAgent, out);
>
>              // Setup XSLT
>             TransformerFactory factory = TransformerFactory.newInstance();
>  Transformer transformer = factory.newTransformer(new StreamSource(new
> File("/Downloads/apache-forrest-0.8/main/webapp/skins/common/xslt/fo/document-to-fo.xsl")));
>
>              // Set the value of a <param> in the stylesheet
>             transformer.setParameter("versionParam", "2.0");
>
>              // Setup input for XSLT transformation
>  Source src = new StreamSource("
> http://xmlgraphics.apache.org/fop/index.xml");
>
>              // Resulting SAX events (the generated FO) must be piped
> through to FOP
>             Result res = new SAXResult(fop.getDefaultHandler());
>
>              // Start XSLT transformation and FOP processing
>             transformer.transform(src, res);
>
>             out.close();
>  }
>  }
>
>
>


-- 
<img src="http://img2.meetupstatic.com/img/logo_tilt_2.gif"/>

Re: Convert xdoc document to PDF

Posted by Ferdinand Soethe <sa...@soethe.net>.
Hi Alan,

as far as I remember the stylesheets  for pdf-generation have moved from 
core into the pdf-output-plugin.

So you may be looking at outdated style sheets. Which might also explain 
the errors you are getting (some changes were required in the 
transformations to accomodate that last fop-update).

Check out 
plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets for 
the current stylesheets (from head).
After using Forrest with the pdf-plugin for the first time, you will 
also find the stylesheets in 
build/plugins/org.apache.forrest.plugin.output.pdf/resources/stylesheets.

Yet I agree with Brian, these style sheets were designed to be used in a 
pipeline, so you may have to make some major adjustments.

Regards,
Ferdinand Soethe

Am 06.07.10 06:27, schrieb Alan Wang:
> Hi All,
>
> I am writing a program similar to pdf output plugin to convert xdoc 
> pages in xml document ( xdoc similar to 
> http://xmlgraphics.apache.org/fop/index.xml), and would like to 
> generate the pdf output using fop (0.95 on Mac os 10.5) as shown in 
> the following.  However I am getting the following errors from console.
>
> (Location of error unknown)org.apache.fop.fo.ValidationException: 
> Error(Unknown location): For fo:simple-page-master, fo:region-body 
> must be declared before fo:region-before.
>
> 1. Where can I find the xsl to convert from xdoc to fo. I find one 
> from apache forrest 
> distribution( apache-forrest-0.8/main/webapp/skins/common/xslt/fo/document-to-fo.xsl)? 
> Or Am I using the wrong one for converting from xdoc to fo?
>
> 2. Is anything wrong with my code?
>
> Regards,
> Alan
>
>
> ========================================================
> public class fop {
> public static void main(String[] args) throws Exception {
> // configure fopFactory as desired
>             FopFactory fopFactory = FopFactory.newInstance();
>             fopFactory.setStrictValidation(false);
>             fopFactory.setStrictUserConfigValidation(false);
>             // configure foUserAgent as desired
>             FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
>
>             // Setup output
>             OutputStream out = new java.io.FileOutputStream(new 
> File("mypdf.pdf"));
>             out = new java.io.BufferedOutputStream(out);
>
>             // Construct fop with desired output format
>             Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, 
> foUserAgent, out);
>
>             // Setup XSLT
>             TransformerFactory factory = TransformerFactory.newInstance();
> Transformer transformer = factory.newTransformer(new StreamSource(new 
> File("/Downloads/apache-forrest-0.8/main/webapp/skins/common/xslt/fo/document-to-fo.xsl")));
>
>             // Set the value of a <param> in the stylesheet
>             transformer.setParameter("versionParam", "2.0");
>
>             // Setup input for XSLT transformation
> Source src = new 
> StreamSource("http://xmlgraphics.apache.org/fop/index.xml");
>
>             // Resulting SAX events (the generated FO) must be piped 
> through to FOP
>             Result res = new SAXResult(fop.getDefaultHandler());
>
>             // Start XSLT transformation and FOP processing
>             transformer.transform(src, res);
>             out.close();
> }
> }


Re: Convert xdoc document to PDF

Posted by Brian M Dube <bd...@apache.org>.
On Mon, Jul 05, 2010 at 09:27:06PM -0700, Alan Wang wrote:
> Hi All,
> 
> I am writing a program similar to pdf output plugin to convert xdoc pages in
> xml document ( xdoc similar to http://xmlgraphics.apache.org/fop/index.xml),
> and would like to generate the pdf output using fop (0.95 on Mac os 10.5) as
> shown in the following.  However I am getting the following errors from
> console.
> 
> (Location of error unknown)org.apache.fop.fo.ValidationException:
> Error(Unknown location): For fo:simple-page-master, fo:region-body must be
> declared before fo:region-before.
> 
> 1. Where can I find the xsl to convert from xdoc to fo. I find one from
> apache forrest
> distribution( apache-forrest-0.8/main/webapp/skins/common/xslt/fo/document-to-fo.xsl)?
> Or Am I using the wrong one for converting from xdoc to fo?

It would take extra steps to use that stylesheet for what you are
trying to accomplish. The stylesheet is not general purpose; that is,
it's part of the following process:

1) skinconf.xml (from the Forrest skin system) is aggregated with the
requested document
2) XInclude elements are processed
3) Links are rewritten
4) Then the document as modified above is passed through
document-to-fo.xsl, with a few more parameters set

To make document-to-fo.xsl a standalone tool, you would need to manage
steps 1-3 yourself. Or drastically modify the stylesheet.

-Brian

> 
> 2. Is anything wrong with my code?
> 
> Regards,
> Alan