xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Milo van der Zee <m...@vanderzee.org>
Subject Re: Help to upgrade code from FOP 1.1 to FOP 2.2
Date Tue, 15 Oct 2019 17:08:25 GMT
Hello,

My code looks like this...

public static synchronized DocResult generateImpl(String outputFormat,
Document document, Node dataNode, String pageCount) {
   log.debug("generateImpl: start");

   if (dataNode == null) {
      return null;
   }

   DocResult docResult = null;

   Source xslfo = generateXslFo(document);

   try {
      // configure fopFactory as desired with the needed resolvers
      ResourceResolver defaultResolver =
ResourceResolverFactory.createDefaultResourceResolver();
      SchemeAwareResourceResolverBuilder resolverBuilder =
ResourceResolverFactory.createSchemeAwareResourceResolverBuilder(defaultResolver);
      resolverBuilder.registerResourceResolverForScheme("image", new
ImageResolver());
      resolverBuilder.registerResourceResolverForScheme("measurementImage",
new MeasurementImageResolver());
      ResourceResolver resolver = resolverBuilder.build();
      FopFactory fopFactory = new FopFactoryBuilder(new
File(".").toURI(), resolver).build();

      FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

      if(MIME_TIFF.equalsIgnoreCase(outputFormat)) {
            // Give TIFF a high compression ratio
            TIFFRenderer renderer = new TIFFRenderer(foUserAgent);
            BitmapRenderingSettings renderingSettings =
renderer.getRenderingSettings();
            renderingSettings.setCompressionMethod("LZW"); // LZW,
JPEG, PACKBITS
            foUserAgent.setRendererOverride(renderer);
      }

      // configure foUserAgent as desired
      foUserAgent.setTargetResolution(document.getPreviewDPI()); //
only used for bitmap targets and not for PDF targets

      ByteArrayOutputStream out = new ByteArrayOutputStream();

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

      // Setup XSLT
      TransformerFactory factory = TransformerFactory.newInstance();
      factory.setErrorListener(new TransformerErrorListener());
      Transformer transformer = factory.newTransformer(xslfo);
      if (transformer == null) return null;
      transformer.setErrorListener(new TransformerErrorListener()); //
log transformer errors / warnings to our log4j logger
      transformer.setParameter("versionParam", "2.0"); // Set the
value of a <param> in the stylesheet
      LocalDateTime now = DateTimeHelper.getNow();
      transformer.setParameter("currentDate",
DateTimeHelper.localDateTimeToString(Format.DATE_FORMAT_2, now));
      transformer.setParameter("currentTime",
DateTimeHelper.localDateTimeToString(Format.TIME_FORMAT_1, now));
      transformer.setParameter("pageCount", pageCount);

      // Use size and margins from the template or from the document?
      Document templateDocument = document.getTemplateDocument();
      Document masterDocument = document;
      if (templateDocument != null) {
         masterDocument = templateDocument;
      }

      transformer.setParameter("pageHeight", masterDocument.getHeight());
      transformer.setParameter("pageWidth", masterDocument.getWidth());
      transformer.setParameter("margin-top", masterDocument.getMarginTop());
      transformer.setParameter("margin-bottom",
masterDocument.getMarginBottom());
      transformer.setParameter("margin-left", masterDocument.getMarginLeft());
      transformer.setParameter("margin-right", masterDocument.getMarginRight());

      // Setup input for XSLT transformation (FOP needs SAX!)
      String dataString = XMLUtils.w3cNodeToString(dataNode);

      SAXBuilder saxBuilder = new SAXBuilder();
      saxBuilder.setErrorHandler(new SaxParserErrorListener());
      org.jdom2.Document dataDocument = saxBuilder.build(new
StringReader(dataString));
      DOMSource src = new DOMSource((new DOMOutputter()).output(dataDocument));

      // 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);
      docResult = new DocResult();
      docResult.setFop(fop);

      docResult.setData(out.toByteArray());
   } catch (Exception e) {
      log.error("Problem generating PDF file (probably an error in one
of the XML strings)", e);
      return null;
   }

   log.debug("generateImpl: end");
   return docResult;
}


Not directly useable but might help.

MAG,
Milo


Op di 15 okt. 2019 om 17:44 schreef Simon Steiner <
simonsteiner1984@gmail.com>:

> Hi,
>
>
>
> There maybe some help in:
>
> https://issues.apache.org/jira/browse/FOP-2480
>
> https://issues.apache.org/jira/browse/FOP-2495
>
>
>
> Thanks
>
>
>
> *From:* Luis Panadero Guardeño <luis.panadero@digibis.com>
> *Sent:* 15 October 2019 16:29
> *To:* fop-users@xmlgraphics.apache.org
> *Cc:* Luis Panadero Guardeño <luis.panadero@digibis.com>
> *Subject:* Re: Help to upgrade code from FOP 1.1 to FOP 2.2
>
>
>
> And how I assign a URIResolver to it ?
>
> El 15/10/19 a las 15:43, Simon Steiner escribió:
>
> Hi,
>
>
>
> You can try using on FOP 2.x
>
> FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
>
>
>
> Thanks
>
>
>
> *From:* Luis Panadero Guardeño <luis.panadero@digibis.com>
> <luis.panadero@digibis.com>
> *Sent:* 15 October 2019 08:52
> *To:* fop-users@xmlgraphics.apache.org
> *Cc:* Luis Panadero Guardeño <luis.panadero@digibis.com>
> <luis.panadero@digibis.com>
> *Subject:* Help to upgrade code from FOP 1.1 to FOP 2.2
>
>
>
> Hi,
>
> I'm trying to upgrade some old code to FOP 2.2. After seeing the upgrade
> examples on the web, I don't have clue how upgrade this code. This os the
> old code that works perfectly on FOP 1.1 :
>
>     public static Result createXmlResult(OutputStream output, URIResolver
> uriResolver)
>         throws FOPException
>     {
>         FopFactory fopFactory = FopFactory.newInstance();
>         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
>         foUserAgent.setURIResolver(uriResolver);
>
>         Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent,
> output);
>         return new SAXResult(fop.getDefaultHandler());
>     }
>
> ¿Any suggestion ?
>
>
>
> --
>
> *Luis Panadero Guardeño*
> Departamento de Informática
> luis.panadero@digibis.com
>
> [image: DIGIBÍS S.L.]
>
> DIGIBÍS S.L.
>
>
>
> C/ Alenza, 4, 5ª planta.
> 28003 Madrid
> Tf. 91 432 08 88 . Fax 91 432 11 13
>
> http://www.digibis.com
>
> Certificado ISO 9001.
> No imprimir si no es necesario. Protejamos el Medio Ambiente
>
> *En cumplimiento de la LOPD y la LSSI, le informamos de que sus datos
> personales son incorporados a un fichero, titularidad de DIGIBÍS, S.L., con
> el fin de ofrecerle información sobre servicios que pueden ser de su
> interés. Podrá ejercitar sus derechos ARCO (de acceso, rectificación,
> cancelación y oposición) mediante un escrito dirigido a  **digibis@digibis.com
> <digibis@digibis.com>*
> * , con copia del DNI o documento identificativo sustitutorio.En caso de
> querer darse de baja pinche aquí
> <digibis@digibis.com?subject=DAR%20DE%20BAJA>. *
>
>
>
> --
>
> *Luis Panadero Guardeño*
> Departamento de Informática
> luis.panadero@digibis.com
>
> [image: DIGIBÍS S.L.]
>
> DIGIBÍS S.L.
>
>
>
> C/ Alenza, 4, 5ª planta.
> 28003 Madrid
> Tf. 91 432 08 88 . Fax 91 432 11 13
>
> http://www.digibis.com
>
> Certificado ISO 9001.
> No imprimir si no es necesario. Protejamos el Medio Ambiente
>
>
> *En cumplimiento de la LOPD y la LSSI, le informamos de que sus datos
> personales son incorporados a un fichero, titularidad de DIGIBÍS, S.L., con
> el fin de ofrecerle información sobre servicios que pueden ser de su
> interés. Podrá ejercitar sus derechos ARCO (de acceso, rectificación,
> cancelación y oposición) mediante un escrito dirigido a
> digibis@digibis.com <digibis@digibis.com> , con copia del DNI o documento
> identificativo sustitutorio.En caso de querer darse de baja pinche aquí
> <digibis@digibis.com?subject=DAR%20DE%20BAJA>. *
>
>
>

Mime
View raw message