#exposes a function or functions to make PDF files.
import os, cStringIO

from settings import RML_DIRECTORY, SAVE_RML

from rlextra.preppy import preppy
from rlextra.rml2pdf import rml2pdf


def createPdfInMemory(**params):
    """Generates the requested brochure.  Returns a byte array.
    
    Caller should either save this to disk or write it into the
    request as preferred.
    """
    
    templatePath = os.path.join(RML_DIRECTORY, 'base.prep')
    mod = preppy.getModule(templatePath)
    
    rml = mod.getOutput(params)
    
    #now convert to PDF, passing it into a cStringIO
    rml2pdf._saveLastestRml(SAVE_RML, rml)
    buf = cStringIO.StringIO()
    rml2pdf.go(rml, outputFileName=buf)
    buf.reset()
    pdfData = buf.read()

    #return PDF
    return pdfData


