Code Snippet

Just another Code Snippet site

Random Post :




Un serveur parfait sous Debian Wheezy (Debian 7) et nginx – ZenZla

Bon bah aujourd’hui c’est du lourd!! après un serveur parfait sous Ubuntu 9.10, qui date un peu, mais toujours d’actualité, et un serveur parfait sous Debian Squeeze (Debian 6.0), voici un nouvel opus de la série “un serveur parfait” et cette fois il est sous Debian Wheezy (Debian 7), avec un serveur « nginx » et l’interface […]

Source: Un serveur parfait sous Debian Wheezy (Debian 7) et nginx – ZenZla

Windows 10 – Quelques conseils pour améliorer les performances – Korben

Si votre Windows 10 se traine un peu, voici quelques astuces pour l’alléger un peu. Vous allez voir, c’est du très classique et c’était à peu près la même chose sur les versions précédentes de Windows. Première étape, faire le ménage dans les applications qui se lancent au démarrage. Faites un clic droit sur la barre > Lire la suite

Source: Windows 10 – Quelques conseils pour améliorer les performances – Korben

[Java] Generate PDF report using FOP (XSL-T/XSL-FO)

    /**
     * Generate a PDF report using FOP Library.
     * @param xstlIn XSL-FO file use to transform data.
     * @param content Data
     * @param response HttpServletResponse used to sendback PDF report
     */
    public void generatePdf(final InputStream xstlIn, final StringWriter content, final HttpServletResponse response) {

        OutputStream out = null;
        try {
            FopFactory fopFactory = FopFactory.newInstance();

            // Setup input
            LOGGER.debug("Setup input");
            byte[] bytes = content.toString().getBytes("UTF-8");
            Source src = new StreamSource(new ByteArrayInputStream(bytes));

            // Setup Transformer
            LOGGER.debug("Setup Transformer");
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer(new StreamSource(xstlIn));

            // Reset/Re-Initialize HttpResponse
            LOGGER.debug("Reset HttpResponse");
            response.reset();
            response.setContentType("application/force-download");
            response.setHeader("Content-Transfer-Encoding", "binary");
            response.setHeader("Content-Disposition", "attachment; filename=\report.pdf\"");
            response.setContentLength(-1); // This will cause the connector to stream the file chunk by chunk.

            // Init outputstream
            LOGGER.debug("Init OutputStream");
            out = new BufferedOutputStream(response.getOutputStream());

            // Setup FOP
            LOGGER.debug("Setup FOP");
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

            // Make sure the XSL transformation's result is piped through to FOP
            LOGGER.debug("Create SAXResult");
            Result res = new SAXResult(fop.getDefaultHandler());

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

        } catch (TransformerConfigurationException e) {
            // TODO : handle exception
        } catch (TransformerException e) {
            // TODO : handle exception
            e.printStackTrace();
        } catch (FOPException e) {
            // TODO : handle exception
            e.printStackTrace();
        } catch (IOException e) {
            // TODO : handle exception
            e.printStackTrace();
        } finally {
            // TODO : close stream
        }
    }

Dependencies :

		<dependency>
			<groupId>org.apache.xmlgraphics</groupId>
			<artifactId>fop</artifactId>
			<version>1.0</version>
			<exclusions>
				<exclusion>
					<artifactId>xml-apis-ext</artifactId>
					<groupId>xml-apis</groupId>
				</exclusion>
				<exclusion>
					<artifactId>xml-apis</artifactId>
					<groupId>xml-apis</groupId>
				</exclusion>
			</exclusions>
		</dependency>

, , ,

Previous Posts Next posts