views:

87

answers:

1

Hi, I have a few general ideas on how I want to do this, but any help/ guidance would be greatly appreciated...

What I am trying to do is: create a front end cms system, VERY, VERY SIMPLE where a report will be generated from i.e. a template, using jquery (drag, drop etc), included in the report will be placeholders where data will be imported into e.g. name, address etc. This data can be changed by different users who have access to the data.

I was thinking I would need to convert this html into xsl-fo format and then generate it into a pdf as xsl-fo will give me a major advantage on custom display of data on pdf, i.e. the data will appear how I want it to. This will also enable me to do a lookup in the xsl-fo using xslt (or something?) to import the latest updated db values. The tool to actually convert from xsl-fo into pdf that looks like it fits my bill is: fo.net. Ultimately I would need to use some code already out there but where I can avoid it, I would want to.

Keep in mind:
1. I need ultimate control over everything (eventually)
2. Free / open source alternatives that are flexible (with source code)

Questions:
1.Is jquery the best thing to use for the cms? As I will be having custom controls which will contain db data or placeholders for data to be imported into
2. Is XSL-FO the best intermediary language to port this template into for rendering/ converting into a PDF?
3. How do I convert html into xsl-fo? Does c#/.net have an api I can look at?
4. Have I overcomplicated things? Any simpler ways to do this?

Many Thanks in advance

NOTE: The html + css on the page may be very complicated/ flexible so I may need to use jquery to add the css inline to the elements, hence why I am thinking of using XSL-FO as I may be able to generate tags that can read this data and place it on the pdf in a certain way, please keep this in mind when answering my question (if you choose to!) :)

A: 

I have found PDFsharp and MigraDoc to be great for pdf generation.

I have created a pdf utility...

using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using PdfSharp.Pdf;

//Controller for a PdfResult
namespace Web.Utilities

{
    public class PdfResult : ActionResult
{

    public String Filename { get; set; }

    protected MemoryStream pdfStream = new MemoryStream();

    public PdfResult(PdfDocument doc)
    {
        Filename = String.Format("{0}.pdf", doc.Info.Title);
        doc.Save(pdfStream, false);
    }

    public PdfResult(String pdfpath)
    {
        /* optional if requried ToString save ToString file System */
        throw new NotImplementedException("PdfResult is just an example and does not serve files from the filesystem.");
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.Clear();
        context.HttpContext.Response.ContentType = "application/pdf";

        context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename); // specify filename

        context.HttpContext.Response.AddHeader("content-length", pdfStream.Length.ToString());
        context.HttpContext.Response.BinaryWrite(pdfStream.ToArray());
        context.HttpContext.Response.Flush();
        pdfStream.Close();
        context.HttpContext.Response.End();
    }

}

}

And then you can render a view of pdf in the controller...

        public ActionResult Download() 
    {
        Document document = new Document();
        document.Info.Title = "Hello";

        Section section = document.AddSection();
        section.AddParagraph("Hello").AddFormattedText("World", TextFormat.Bold);

        PdfDocumentRenderer renderer = new PdfDocumentRenderer();
        renderer.Document = document;
        renderer.RenderDocument();

        return new PdfResult(renderer.PdfDocument);
    }

I have found this to be a really neat and easy to control method of putting pdf into mvc.

Desiny
Hi Desiny,Thanks for your quick reply, your code looks really good! I am still not convinced that it is what I require, I had a quick look into pdf sharp- how did you find this tool for dynamic html and css when creating PDF's? The major part of my requirement is to create a simple template/cms which can have divs aligned to left/ right, headers with bold text, inline styles etc. (which will be done using jquery)- looking at the above code I would to map the possible html + css possibilities in the template then set each section/ paragraph with the styles, for each element/ div on the page???
Haroon