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 DIALLO Yaya <Ya...@sgam.com> on 2007/03/06 16:57:19 UTC

FOP 0.93 generated a damaged PDF File

Hi,

 

I use FOP 0.93 to generate a pdf file from xml/XSL-FO. While opening,
the file I get the following error, unable to open the file , the file
is damaged.

 

I past below my code

 

public static String postPDF(final FOPTransformable toTransform) throws
BindingException {

            // convert tree content in XML file : marshall

            final String xml = XMLBinding.marshall(toTransform);

 

            // obtain the XSL file

            final InputStream xslStream;

            try {

                  if (logger.isDebugEnabled()) logger.debug("looking for
xsl stylesheet...");

                  xslStream =
XMLBinding.class.getClassLoader().getResourceAsStream(PropertyService.ge
tXMLProperty(toTransform.getKey()));

            } catch (PropertyServiceException e) {

                  throw new BindingException(e);

            }

 

            OutputStream out = null;

            BufferedReader in = null;

      

            try {

 

                  StringBuffer relativeUrl = new StringBuffer(64);

 
relativeUrl.append(PropertyService.getXMLProperty("biz.publication.ticke
tdir"));

                  relativeUrl.append(toTransform.getGeneratedPath());

 
relativeUrl.append(toTransform.getGeneratedFileName()).append(".pdf");

 

                  File file = new
File(PropertyService.getXMLProperty("biz.publication.basedir") +
relativeUrl);

 

 

                  

                  final HttpURLConnection con =
UploadFile.initConnection(file.getParent(), file.getName());

//                

                  out = con.getOutputStream();

                  

                  //out = con.getOutputStream();

                  FopFactory fopFactory = FopFactory.newInstance();

                  final Fop fop =
fopFactory.newFop(MimeConstants.MIME_PDF, out);

                  //driver.setOutputStream(out);

                        

 

                  //Setup XSLT

                  final TransformerFactory factory =
TransformerFactory.newInstance();

                  final Transformer transformer =
factory.newTransformer(new StreamSource(xslStream));

 

                  // set the parameters

                  Set params = toTransform.getParameters();

                  if (params != null) {

                        for (Iterator it = params.iterator();
it.hasNext();) {

                             Parameter p = (Parameter) it.next();

                             transformer.setParameter(p.getName(),
p.getValue());

                        }

                  } 

                                   

                  //Setup input for XSLT transformation

                  final StringReader stringReader = new
StringReader(xml);

                  final Source src = new StreamSource(stringReader);

 

                  //Resulting SAX events (the generated FO) must be
piped through to FOP

                  final Result res = new
SAXResult(fop.getDefaultHandler());

 

                  //Start XSLT transformation and FOP processing

transformer.transform(src, res);

 

                        

                  // Read the response

                  if (logger.isDebugEnabled()) logger.debug("treat
servlet response");

                  in = new BufferedReader(new
InputStreamReader(con.getInputStream()));

                  treatServletResponse(in);

 

                  return relativeUrl.toString();

            } catch (Exception e) {

                  throw new BindingException(e);

            } finally {

                  if (in != null) {

                        try {

                             in.close();

                        } catch (IOException e) {

                             // nothing to do

                        }

                  }

                  if (out != null) {

                        try {

                             out.close();

                        } catch (IOException e) {

                             // nothing to do

                        }

                  }

                  

            }

      }

 

 

public static void treatServletResponse(BufferedReader in) throws
IOException, UploadException {

        // Read and display the response:

        String line = "";

        StringBuffer buf = new StringBuffer(128);

        while ((line = in.readLine()) != null) {

            final FWKLogger logger =
FWKLogger.getLogger(UploadFile.class.getName());

            if (logger.isInfoEnabled()) {

                logger.info("FileUploadServlet returned:");

                logger.info(line);

            }

            buf.append(line);

        }

        in.close();

 

        String response = buf.toString();

        if (response.startsWith(FAILURE)) {

            throw new UploadException(response);

        } else if (!response.startsWith(SUCCESS)) {

            throw new UploadException("Servlet returned an unexpected
value!");

        }

    }

 

Thx for your help.

Yaya

Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. 
Toute utilisation ou diffusion non autorisee est interdite. 
Tout message electronique est susceptible d'alteration. 
Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 
  
Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr 
  
                                ******** 
  
This message and any attachments (the "message") are confidential and intended solely for the addressees. 
Any unauthorised use or dissemination is prohibited. 
E-mails are susceptible to alteration. 
Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
 
Find out more about Societe Generale Asset Management's proposal on www.sgam.com

Re: FOP 0.93 generated a damaged PDF File

Posted by liam grimes <li...@gmail.com>.
Hi try to do an out.flush() before you close it... so just add out.flush();
before out.close();. it might solve your problem.

On 3/6/07, DIALLO Yaya <Ya...@sgam.com> wrote:
>
>  Hi,
>
>
>
> I use FOP 0.93 to generate a pdf file from xml/XSL-FO. While opening, the
> file I get the following error, unable to open the file , the file is
> damaged.
>
>
>
> I past below my code
>
>
>
> *public* *static* String postPDF(*final* FOPTransformable toTransform) *
> throws* BindingException {
>
>             // convert tree content in XML file : marshall
>
>             *final* String xml = XMLBinding.*marshall*(toTransform);
>
>
>
>             // obtain the XSL file
>
>             *final* InputStream xslStream;
>
>             *try* {
>
>                   *if* (*logger*.isDebugEnabled()) *logger*.debug("looking
> for xsl stylesheet...");
>
>                   xslStream = XMLBinding.*class*
> .getClassLoader().getResourceAsStream(PropertyService.*getXMLProperty*(
> toTransform.getKey()));
>
>             } *catch* (PropertyServiceException e) {
>
>                   *throw* *new* BindingException(e);
>
>             }
>
>
>
>             OutputStream out = *null*;
>
>             BufferedReader in = *null*;
>
>
>
>             *try* {
>
>
>
>                   StringBuffer relativeUrl = *new* StringBuffer(64);
>
>                   relativeUrl.append(PropertyService.*getXMLProperty*("
> biz.publication.ticketdir"));
>
>                   relativeUrl.append(toTransform.getGeneratedPath());
>
>                   relativeUrl.append(toTransform.getGeneratedFileName
> ()).append(".pdf");
>
>
>
>                   File file = *new* File(PropertyService.*getXMLProperty*(
> "biz.publication.basedir") + relativeUrl);
>
>
>
>
>
>
>
>                   *final* HttpURLConnection con = UploadFile.*
> initConnection*(file.getParent(), file.getName());
>
> //
>
>                   out = con.getOutputStream();
>
>
>
>                   //out = con.getOutputStream();
>
>                   FopFactory fopFactory = FopFactory.*newInstance*();
>
>                   *final* Fop fop = fopFactory.newFop(MimeConstants.*
> MIME_PDF*, out);
>
>                   //driver.setOutputStream(out);
>
>
>
>
>
>                   //Setup XSLT
>
>                   *final* TransformerFactory factory = TransformerFactory.
> *newInstance*();
>
>                   *final* Transformer transformer = factory.newTransformer
> (*new* StreamSource(xslStream));
>
>
>
>                   // set the parameters
>
>                   Set params = toTransform.getParameters();
>
>                   *if* (params != *null*) {
>
>                         *for* (Iterator it = params.iterator(); it.hasNext();)
> {
>
>                              Parameter p = (Parameter) it.next();
>
>                              transformer.setParameter(p.getName(),
> p.getValue());
>
>                         }
>
>                   }
>
>
>
>                   //Setup input for XSLT transformation
>
>                   *final* StringReader stringReader = *new*StringReader(xml);
>
>                   *final* Source src = *new* StreamSource(stringReader);
>
>
>
>                   //Resulting SAX events (the generated FO) must be piped
> through to FOP
>
>                   *final* Result res = *new* SAXResult(
> fop.getDefaultHandler());
>
>
>
>                   //Start XSLT transformation and FOP processing
>
> transformer.transform(src, res);
>
>
>
>
>
>                   // Read the response
>
>                   *if* (*logger*.isDebugEnabled()) *logger*.debug("treat
> servlet response");
>
>                   in = *new* BufferedReader(*new* InputStreamReader(
> con.getInputStream()));
>
>                   *treatServletResponse*(in);
>
>
>
>                   *return* relativeUrl.toString();
>
>             } *catch* (Exception e) {
>
>                   *throw* *new* BindingException(e);
>
>             } *finally* {
>
>                   *if* (in != *null*) {
>
>                         *try* {
>
>                              in.close();
>
>                         } *catch* (IOException e) {
>
>                              // nothing to do
>
>                         }
>
>                   }
>
>                   *if* (out != *null*) {
>
>                         *try* {
>
>                              out.close();
>
>                         } *catch* (IOException e) {
>
>                              // nothing to do
>
>                         }
>
>                   }
>
>
>
>             }
>
>       }
>
>
>
>
>
> *public* *static* *void* treatServletResponse(BufferedReader in) *throws*IOException, UploadException {
>
>         // Read and display the response:
>
>         String line = "";
>
>         StringBuffer buf = *new* StringBuffer(128);
>
>         *while* ((line = in.readLine()) != *null*) {
>
>             *final* FWKLogger logger = FWKLogger.*getLogger*(UploadFile.*
> class*.getName());
>
>             *if* (logger.isInfoEnabled()) {
>
>                 logger.info("FileUploadServlet returned:");
>
>                 logger.info(line);
>
>             }
>
>             buf.append(line);
>
>         }
>
>         in.close();
>
>
>
>         String response = buf.toString();
>
>         *if* (response.startsWith(*FAILURE*)) {
>
>             *throw* *new* UploadException(response);
>
>         } *else* *if* (!response.startsWith(*SUCCESS*)) {
>
>             *throw* *new* UploadException("Servlet returned an unexpected
> value!");
>
>         }
>
>     }
>
>
>
> Thx for your help.
>
> Yaya
>
> Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires.
> Toute utilisation ou diffusion non autorisee est interdite.
> Tout message electronique est susceptible d'alteration.
> Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie.
>
> Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr
>
>                                 ********
>
> This message and any attachments (the "message") are confidential and intended solely for the addressees.
> Any unauthorised use or dissemination is prohibited.
> E-mails are susceptible to alteration.
> Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified.
>
> Find out more about Societe Generale Asset Management's proposal on www.sgam.com
>
>

Re: FOP 0.93 generated a damaged PDF File

Posted by Adrian Cumiskey <ad...@gmail.com>.
Hi Yaya,

Are you using FOP 0.20 or 0.93?  Please post up your input xml/XSL-FO 
files and I'll take a look at it.

Adrian.

DIALLO Yaya wrote:
> Hi,
> 
>  
> 
> I use FOP 0.93 to generate a pdf file from xml/XSL-FO. While opening, 
> the file I get the following error, unable to open the file , the file 
> is damaged.
> 
>  
> 
> I past below my code
> 
>  
> 
> *public* *static* String postPDF(*final* FOPTransformable toTransform) 
> *throws* BindingException {
> 
>             // convert tree content in XML file : marshall
> 
>             *final* String xml = XMLBinding./marshall/(toTransform);
> 
>  
> 
>             // obtain the XSL file
> 
>             *final* InputStream xslStream;
> 
>             *try* {
> 
>                   *if* (/logger/.isDebugEnabled()) 
> /logger/.debug("looking for xsl stylesheet...");
> 
>                   xslStream = 
> XMLBinding.*class*.getClassLoader().getResourceAsStream(PropertyService./getXMLProperty/(toTransform.getKey()));
> 
>             } *catch* (PropertyServiceException e) {
> 
>                   *throw* *new* BindingException(e);
> 
>             }
> 
>  
> 
>             OutputStream out = *null*;
> 
>             BufferedReader in = *null*;
> 
>      
> 
>             *try* {
> 
>  
> 
>                   StringBuffer relativeUrl = *new* StringBuffer(64);
> 
>                   
> relativeUrl.append(PropertyService./getXMLProperty/("biz.publication.ticketdir"));
> 
>                   relativeUrl.append(toTransform.getGeneratedPath());
> 
>                   
> relativeUrl.append(toTransform.getGeneratedFileName()).append(".pdf");
> 
>  
> 
>                   File file = *new* 
> File(PropertyService./getXMLProperty/("biz.publication.basedir") + 
> relativeUrl);
> 
>  
> 
>  
> 
>                  
> 
>                   *final* HttpURLConnection con = 
> UploadFile./initConnection/(file.getParent(), file.getName());
> 
> //               
> 
>                   out = con.getOutputStream();
> 
>                  
> 
>                   //out = con.getOutputStream();
> 
>                   FopFactory fopFactory = FopFactory./newInstance/();
> 
>                   *final* Fop fop = 
> fopFactory.newFop(MimeConstants./MIME_PDF/, out);
> 
>                   //driver.setOutputStream(out);
> 
>                        
> 
>  
> 
>                   //Setup XSLT
> 
>                   *final* TransformerFactory factory = 
> TransformerFactory./newInstance/();
> 
>                   *final* Transformer transformer = 
> factory.newTransformer(*new* StreamSource(xslStream));
> 
>  
> 
>                   // set the parameters
> 
>                   Set params = toTransform.getParameters();
> 
>                   *if* (params != *null*) {
> 
>                         *for* (Iterator it = params.iterator(); 
> it.hasNext();) {
> 
>                              Parameter p = (Parameter) it.next();
> 
>                              transformer.setParameter(p.getName(), 
> p.getValue());
> 
>                         }
> 
>                   }
> 
>                                   
> 
>                   //Setup input for XSLT transformation
> 
>                   *final* StringReader stringReader = *new* 
> StringReader(xml);
> 
>                   *final* Source src = *new* StreamSource(stringReader);
> 
>  
> 
>                   //Resulting SAX events (the generated FO) must be 
> piped through to FOP
> 
>                   *final* Result res = *new* 
> SAXResult(fop.getDefaultHandler());
> 
>  
> 
>                   //Start XSLT transformation and FOP processing
> 
> transformer.transform(src, res);
> 
>  
> 
>                        
> 
>                   // Read the response
> 
>                   *if* (/logger/.isDebugEnabled()) /logger/.debug("treat 
> servlet response");
> 
>                   in = *new* BufferedReader(*new* 
> InputStreamReader(con.getInputStream()));
> 
>                   /treatServletResponse/(in);
> 
>  
> 
>                   *return* relativeUrl.toString();
> 
>             } *catch* (Exception e) {
> 
>                   *throw* *new* BindingException(e);
> 
>             } *finally* {
> 
>                   *if* (in != *null*) {
> 
>                         *try* {
> 
>                              in.close();
> 
>                         } *catch* (IOException e) {
> 
>                              // nothing to do
> 
>                         }
> 
>                   }
> 
>                   *if* (out != *null*) {
> 
>                         *try* {
> 
>                              out.close();
> 
>                         } *catch* (IOException e) {
> 
>                              // nothing to do
> 
>                         }
> 
>                   }
> 
>                  
> 
>             }
> 
>       }
> 
>  
> 
>  
> 
> *public* *static* *void* treatServletResponse(BufferedReader in) 
> *throws* IOException, UploadException {
> 
>         // Read and display the response:
> 
>         String line = "";
> 
>         StringBuffer buf = *new* StringBuffer(128);
> 
>         *while* ((line = in.readLine()) != *null*) {
> 
>             *final* FWKLogger logger = 
> FWKLogger./getLogger/(UploadFile.*class*.getName());
> 
>             *if* (logger.isInfoEnabled()) {
> 
>                 logger.info("FileUploadServlet returned:");
> 
>                 logger.info(line);
> 
>             }
> 
>             buf.append(line);
> 
>         }
> 
>         in.close();
> 
>  
> 
>         String response = buf.toString();
> 
>         *if* (response.startsWith(/FAILURE/)) {
> 
>             *throw* *new* UploadException(response);
> 
>         } *else* *if* (!response.startsWith(/SUCCESS/)) {
> 
>             *throw* *new* UploadException("Servlet returned an 
> unexpected value!");
> 
>         }
> 
>     }
> 
>  
> 
> Thx for your help.
> 
> Yaya
> 
> Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. 
> Toute utilisation ou diffusion non autorisee est interdite. 
> Tout message electronique est susceptible d'alteration. 
> Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 
>   
> Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr 
>   
>                                 ******** 
>   
> This message and any attachments (the "message") are confidential and intended solely for the addressees. 
> Any unauthorised use or dissemination is prohibited. 
> E-mails are susceptible to alteration. 
> Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
>  
> Find out more about Societe Generale Asset Management's proposal on www.sgam.com


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