Thanks so much!

I use magnolia 4.4.2 and subtemplates work with templatePath and type
definition like:
templatePath: /html2pdf
type: servlet

In attach my piece of code! It's cool! :)
(Yes, I must refactor my code... but it's my two cents to community :) )

Thanks!
SC

On Tue, 2011-04-05 at 16:07 +0200, Thomas Martin wrote:
> Hi Scrivo
> 
> I've attached you the source for a bare-bone servlet that generates entries 
> found in NodeDatas called "title", "text" and "image" found under 
> "mainColumnParagraphs".
> 
> I've tested it against 4.4.2 of ce today and it worked.
> 
> Two things that need to be in place: 
> 
> - subtemplate as in my previous email
> - servlet definition as follows:
> 
> 
> 
> ----------------------------------------------------------------
> For list details see
> http://www.magnolia-cms.com/home/community/mailing-lists.html
> To unsubscribe, E-mail to: <[email protected]>
> ----------------------------------------------------------------
> 
> 
> Hope this helps.
> Cheers, Thomas
> 
> @Jan, re(have you thought of maybe sharing your servlet with others as a 
> separate module on forge?)
> I'm leaving on thursday for holidays (yay!) so my schedule is unfortunately 
> quite intense - I honestly will try to do so upon return.
> 
> 
> 
> 
> On 04.04.2011, at 15:36, Codice Scrivo wrote:
> 
> > 
> > Thanks for help, but it seems that solution does't work for me.
> > 
> > I use magnolia 4.4.2 and subtemplates with configuration posted is never
> > called.
> > By the way, the servlet is properly configured and respond on request.
> > 
> > I try to found another solution, but my problem is how make my jsp to
> > pdf?
> > In other words: I haven't the html page rendered, i think that i need
> > get the page with stream or not?
> > Do I need to write a pdf renderer or a pdf template?
> > 
> > I try flying-saucer, but the problem remain the same: how can
> > I read the html filtered by magnolia?
> > 
> > Thanks!
> > SC
> > 
> > 
> > ----------------------------------------------------------------
> > For list details see
> > http://www.magnolia-cms.com/home/community/mailing-lists.html
> > To unsubscribe, E-mail to: <[email protected]>
> > ----------------------------------------------------------------
> > 
> 



----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/home/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------
package scrivo.codice

import info.magnolia.cms.core.Content;
import info.magnolia.context.MgnlContext;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.PdfWriter;

public class Html2Pdf extends HttpServlet
{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        this.doPost(request, response);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
        IOException
    {
        try
        {
            Document document = new Document();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            Content content = MgnlContext.getAggregationState().getCurrentContent();

            PdfWriter.getInstance(document, baos);
            document.open();
            StyleSheet styles = new StyleSheet();
            styles.loadTagStyle("body", "font", "Bitstream Vera Sans");
            styles.loadTagStyle("div", "color", "red");

            StringBuffer html = new StringBuffer();

            html.append("<h1>").append(content.getTitle()).append("</h1>");

            for (Content child : content.getContent("main").getChildren())
            {

                html.append(String.format("<div><h2>%1$s<h2><div>%2$s</div></div>", child
                    .getNodeData("paragTitle")
                    .getString(), child.getNodeData("paragAbstract").getString()));
            }

            List<Element> arrayElementList = HTMLWorker.parseToList(new StringReader(html.toString()), styles);
            for (Element element : arrayElementList)
            {
                document.add(element);
            }

            document.close();

            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            response.setContentType("application/pdf");
            response.addHeader(
                "Content-Disposition",
                "attachment; filename=" + String.format("%s.pdf", content.getTitle().replaceAll("\\s", "_")));
            ServletOutputStream os = response.getOutputStream();

            baos.writeTo(os);

            os.flush();
            os.close();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Reply via email to