def _generate_pdf(self, rst_file_path, result_pdf_path):
        """
        Generates a PDF file based on the input RST file.
        """
        if not os.path.exists(rst_file_path):
            raise DocGenerateException(
                "Provided RST file: %s does not exists." % rst_file_path)

        # We suppose all RST files have references relative to their folder
        # So we have to force rst2pdf to be executed in the folder where RST file resides.
        basedir = os.path.split(rst_file_path)[0]
        baseurl = "file://" + basedir

        r2p = RstToPdf(style_path=[self._styles_folder],
                       stylesheets=[self.PDF_STYLE],
                       basedir=basedir,
                       baseurl=baseurl,
                       breakside="any")
        try:
            with open(rst_file_path) as file_rst:
                content = file_rst.read()
            r2p.createPdf(text=content,
                          source_path=rst_file_path,
                          output=result_pdf_path)
            self.logger.debug("PDF file %s generated successfully." %
                              result_pdf_path)
        except Exception, ex:
            self.logger.exception("Could not generate PDF documentation")
            raise DocGenerateException("Could not generate PDF documentation",
                                       ex)
Beispiel #2
0
def run():
    inpdir=os.path.abspath('./input')
    outdir=os.path.abspath('./tmp')
    # Discard output, this is not about whether things 
    # work or not, that's testing ;-)

    for f in os.listdir(inpdir):
        if f.endswith('.txt'): # Test case
            print 'Running: ', f
            sheet=os.path.join(inpdir, f[:-4]+'.style')
            if os.path.exists(sheet):
                sheet=[sheet]
            else:
                sheet=[]
            
            r2p=RstToPdf(stylesheets=sheet)
            try:
                fname=os.path.join(inpdir, f)
                r2p.createPdf(
                    text=open(fname).read(),
                    output=os.path.join(outdir,f+'.pdf'),
                    source_path=fname,
                    )
            except:
                print 'FAIL'
Beispiel #3
0
def renderQueue(render_queue, pdf_queue, doctree_queue):
    _renderer = RstToPdf(splittables=True)

    def render(doctree, preview=True):
        '''Render text to PDF via rst2pdf'''
        # FIXME: get parameters for this from somewhere

        sio=StringIO()
        _renderer.createPdf(doctree=doctree, output=sio, debugLinesPdf=preview)
        return sio.getvalue()

    while True:
        try:
            style_file, text, preview = render_queue.get(10)
            style_file, text, preview = render_queue.get(False)
        except Empty: # no more things to render, so do it
            try:
                if style_file:
                    _renderer.loadStyles([style_file])
                flag = True
                #os.unlink(style_file)
                warnings=StringIO()
                doctree = docutils.core.publish_doctree(text,
                    settings_overrides={'warning_stream':warnings})
                doctree_queue.put([doctree,warnings.getvalue()])
                pdf_queue.put(render(doctree, preview))
            except Exception as e:
                # Don't crash ever ;-)
                print(e)
                pass
        if os.getppid()==1: # Parent died
            sys.exit(0)
Beispiel #4
0
 def __init__(self, *args, **kwargs):
     try:
         from rst2pdf.createpdf import RstToPdf
         self.pdfcreator = RstToPdf(breakside=0,
                                    stylesheets=['twelvepoint'])
     except ImportError:
         raise Exception("unable to find rst2pdf")
     super(PdfGenerator, self).__init__(*args, **kwargs)
Beispiel #5
0
 def __init__(self, *args, **kwargs):
     super(PdfGenerator, self).__init__(*args, **kwargs)
     
     pdf_style_path = os.path.join(self.settings['PDF_STYLE_PATH'])
     pdf_style = self.settings['PDF_STYLE']
     self.pdfcreator = RstToPdf(breakside=0,
                                stylesheets=[pdf_style],
                                style_path=[pdf_style_path])
Beispiel #6
0
            def download(self, pageid):
                p = model.FlatPage.by_id(pageid) or abort(404)
                out = BytesIO()

                rst2pdf = RstToPdf()
                rst2pdf.createPdf(p.content, output=out)

                out.seek(0)
                return out.read()
Beispiel #7
0
def rst2pdf(content, theme=None):
    topdf = RstToPdf(basedir=current_app.config.root_path, breaklevel=0)
    buf = BytesIO()

    if not content:
        content = '\0'
    content_utf8 = utf8codec.encode(content)[0]
    topdf.createPdf(text=content_utf8, output=buf, compressed=False)

    return buf.getvalue()
Beispiel #8
0
 def __init__(self, *args, **kwargs):
     super(PdfGenerator, self).__init__(*args, **kwargs)
     try:
         from rst2pdf.createpdf import RstToPdf
         pdf_style_path = os.path.join(self.settings['PDF_STYLE_PATH'])
         pdf_style = self.settings['PDF_STYLE']
         self.pdfcreator = RstToPdf(breakside=0,
                                    stylesheets=[pdf_style],
                                    style_path=[pdf_style_path])
     except ImportError:
         raise Exception("unable to find rst2pdf")
    def generate_random_act_attach(cls, act, n=1):
        """
        generates n random pdf attachments for an act

        title and body, are built using a lorem ipsum generator

        the attachments are built with text and a pdf file upload is simulated
        """
        # random title and body generation
        g = lipsum.MarkupGenerator()

        for i in range(1, n + 1):
            title = g.generate_sentences_plain(1)
            body = g.generate_paragraphs_plain(random.randint(3, 50),
                                               start_with_lorem=True)

            # attach object created and saved
            attach = Attach(act=act, title=title, text=body)
            attach.save()

            #
            # pdf document generation and upload in proper directory
            #

            # document setup
            if act is not None:
                header = "Atto %s - ###Title###" % (act.idnum, )
            else:
                header = "###Title###"
            footer = "###Section### - Pagina: ###Page###"
            text = "%s\n%s\n%s\n" % ("=" * int(len(title) * 1.5), title,
                                     "=" * int(len(title) * 1.5))
            text += body

            # pdf saved into tmp file
            file_name = "%s_%s.pdf" % (act.idnum, attach.id)
            tmp_file = os.path.join("/tmp", file_name)
            rst2pdf = RstToPdf(breaklevel=0, header=header, footer=footer)
            rst2pdf.createPdf(text=text, output=tmp_file)

            # file is saved as attribute of the attach object and moved in the right path
            # see https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FieldFile.save
            f = open(tmp_file, 'r')
            attach.file.save(file_name, File(f))
            attach.save()
            os.remove(tmp_file)

            print "%s: %s uploaded" % (i, file_name)
Beispiel #10
0
def get_submission_confirmation_report(submission):
    sections = []
    for section, fields in SUBMIT_CONFIRMATION:
        cv_items = []
        for field in fields:
            model_field = Submission._meta.get_field_by_name(field)[0]
            name = model_field.verbose_name
            if getattr(model_field, 'choices', None):
                value = getattr(submission, 'get_%s_display' % field)()
            else:
                value = getattr(submission, field)
            cv_items.append((name, value))

        sections.append((section, cv_items))

    permissions = []
    for field in SUBMIT_CONFIRMATION_PERMISSIONS:
        value = getattr(submission, field)
        if value != 1:
            continue

        model_field = Submission._meta.get_field_by_name(field)[0]
        name = model_field.verbose_name
        permissions.append(name)
    if permissions:
        permissions = [(_('Permissions'), permissions)]

    docs = os.path.join(APP_ROOT, 'docs')
    lang = translation.get_language().lower()
    submission_rst = os.path.join(
        docs, SUBMISSION_RST.get(lang, DEFAULT_SUBMISSION_RST))
    with io.open(submission_rst, encoding='utf-8') as submission_head:
        rst_data = submission_head.read() % _get_rst_fields(
            sections, permissions)

    from rst2pdf.createpdf import RstToPdf
    pdf_creator = RstToPdf(
        stylesheets=[os.path.join(docs, 'submission2.stylesheet')],
        font_path=['/usr/share/fonts/TTF/'],
        breaklevel=0,
    )

    with io.open('/tmp/cpm.rst', 'w', encoding='utf-8') as w:
        w.write(rst_data)
    pdf_content = StringIO()
    pdf_creator.createPdf(text=rst_data, output=pdf_content)
    return pdf_content.getvalue()
Beispiel #11
0
    def __init__(self, *args, **kwargs):
        super(PdfGenerator, self).__init__(*args, **kwargs)

        if "PDF_STYLE_PATH" in self.settings:
            pdf_style_path = [self.settings["PDF_STYLE_PATH"]]
        else:
            pdf_style_path = []

        if "PDF_STYLE" in self.settings:
            pdf_style = [self.settings["PDF_STYLE"]]
        else:
            pdf_style = []

        self.pdfcreator = RstToPdf(breakside=0,
                                   stylesheets=pdf_style,
                                   style_path=pdf_style_path,
                                   raw_html=True)
Beispiel #12
0
    def test_rst2pdf(self):

        assert RstToPdf, 'rst2pdf not installed - cannot test compatibility.'

        rst2pdf = RstToPdf(breaklevel=0)

        rst2pdf.styles['heading1'].spaceBefore = 36

        buffer = BytesIO()

        rst2pdf.createPdf(text, output=buffer)

        pdf = buffer.getvalue()
        buffer.close()

        assert b'ReportLab generated PDF document' in pdf
        assert b'Rst2Pdf MultiBuild Test Case For Wordaxe' in pdf
        assert b'Test Section' in pdf
        assert blah.encode() in pdf
Beispiel #13
0
def produce_pdf(rst_content=None, doctree_content=None, filename=None):
    """produce a pdf content based of a given rst content

    If filename is given, it will store the result using the given filename
    if no filename is given, it will generate a pdf in /tmp/ with a random
    name
    """
    if filename is None:
        filename = os.path.join(
            "/tmp", ''.join([
                random.choice(string.ascii_letters + string.digits)
                for n in range(15)
            ]) + '.pdf')
    r2p = RstToPdf(
        stylesheets=['pdf.style'],
        style_path=[os.path.join(os.path.dirname(__file__), 'styles')],
        breaklevel=0,
        splittables=True,
        footer="""###Title### - ###Page###/###Total###""")
    r2p.createPdf(text=rst_content, doctree=doctree_content, output=filename)
    return filename
Beispiel #14
0
def splitn_command(secret, n, out_file, secret_name, **kwargs):
    """
    N shares so that any two shares can be combined to obtain the secret.
    Each share will contain n-1 share values, each matching a different share.
    The secret can be obtained by combining two share values with the same
    share ID.

    The share tables and instructions are saved to pdf.
    """

    if secret is None:
        secret = getpass(prompt='Secret: ')

    ids = list(range(100, 999))
    n_share_values = (n * n) / 2 - n
    if len(ids) < n_share_values:
        raise ValueError(
            f"Share ID space too small for n={n}. Choose smaller n.")
    shuffle(ids)

    shares = [[] for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            share_a, share_b = split_secret(secret)
            share_id = ids.pop()
            shares[i].append([j + 1, f'#{share_id}', f'``{share_a}``'])
            shares[j].append([i + 1, f'#{share_id}', f'``{share_b}``'])

    pages = [
        create_page(secret_name=secret_name,
                    sheet_id=i,
                    n_sheet=n,
                    records=share) for i, share in enumerate(shares, 1)
    ]
    rst_doc = join_pages(pages)

    # with open('foo.rst', 'w') as fh:
    #     fh.write(rst_doc)

    RstToPdf().createPdf(text=rst_doc, output=out_file)
Beispiel #15
0
#!/usr/bin/env python2
import subprocess
import jinja2
from rst2pdf.createpdf import RstToPdf
import tabulate
import yaml

with open("secrets.yaml", 'r') as stream:
    secrets = yaml.load(stream)

table = [
    [secrets['address'], secrets['email']],
    [secrets['phone'], "http://bradym.net"]
]

rst_table = tabulate.tabulate(table, tablefmt='rst')

rendered = jinja2.Environment(loader=jinja2.FileSystemLoader('./'))\
    .get_template('resume.rst').render({'table': rst_table})

RstToPdf(stylesheets=['style.json'], breaklevel=0).createPdf(rendered, output='resume.pdf')

subprocess.call('open resume.pdf', shell=True)
Beispiel #16
0
 def setUp(self):
     self.converter=RstToPdf()
Beispiel #17
0
    def summary_cover(self, sub_id=None, out_file=None):
        """ Generates a cover page with subject information """
        from mriqc import __version__
        import datetime
        import numpy as np
        from logging import CRITICAL
        from rst2pdf.createpdf import RstToPdf
        from rst2pdf.log import log
        import pkg_resources as pkgr

        failed = self.failed
        if failed is None:
            failed = []

        log.setLevel(CRITICAL)
        newdf = self.dataframe.copy()

        # Format the size
        #pylint: disable=E1101
        newdf[['size_x', 'size_y', 'size_z']] = newdf[['size_x', 'size_y', 'size_z']].astype(np.uint16)
        formatter = lambda row: '%d \u00D7 %d \u00D7 %d' % (
            row['size_x'], row['size_y'], row['size_z'])
        newdf['size'] = newdf[['size_x', 'size_y', 'size_z']].apply(formatter, axis=1)

        # Format spacing
        newdf[['spacing_x', 'spacing_y', 'spacing_z']] = newdf[[
            'spacing_x', 'spacing_y', 'spacing_z']].astype(np.float32)  #pylint: disable=E1101
        formatter = lambda row: '%.3f \u00D7 %.3f \u00D7 %.3f' % (
            row['spacing_x'], row['spacing_y'], row['spacing_z'])
        newdf['spacing'] = newdf[['spacing_x', 'spacing_y', 'spacing_z']].apply(formatter, axis=1)

        # columns
        cols = ['session_id', 'run_id', 'size', 'spacing']
        colnames = ['Session', 'Run', 'Size', 'Spacing']
        if 'tr' in newdf.columns.ravel():
            cols.append('tr')
            colnames.append('TR (sec)')
        if 'size_t' in newdf.columns.ravel():
            cols.append('size_t')
            colnames.append('# Timepoints')

        # Format parameters table
        if sub_id is None:
            cols.insert(0, 'subject_id')
            colnames.insert(0, 'Subject')
        else:
            newdf = newdf[newdf.subject_id == sub_id]

        newdf = newdf[cols]

        colsizes = []
        for col, colname in zip(cols, colnames):
            try:
                newdf[[col]] = newdf[[col]].astype(str)
            except NameError:
                newdf[[col]] = newdf[[col]].astype(str)

            colsize = np.max([len('{}'.format(val)) for val in newdf.loc[:, col]])
            # colsize = newdf.loc[:, col].map(len).max()
            colsizes.append(colsize if colsize > len(colname) else len(colname))

        colformat = ' '.join('{:<%d}' % c for c in colsizes)
        formatter = lambda row: colformat.format(*row)
        rowsformatted = newdf[cols].apply(formatter, axis=1).ravel().tolist()
        # rowsformatted = [formatter.format(*row) for row in newdf.iterrows()]
        header = colformat.format(*colnames)
        sep = colformat.format(*['=' * c for c in colsizes])
        ptable = '\n'.join([sep, header, sep] + rowsformatted + [sep])

        title = 'MRIQC: %s MRI %s report' % (
            self.qctype, 'group' if sub_id is None else 'individual')

        # Substitution dictionary
        context = {
            'title': title + '\n' + ''.join(['='] * len(title)),
            'timestamp': datetime.datetime.now().strftime("%Y-%m-%d, %H:%M"),
            'version': __version__,
            'failed': failed,
            'imparams': ptable
        }

        if sub_id is not None:
            context['sub_id'] = sub_id

        if sub_id is None:
            template = ConfigGen(pkgr.resource_filename(
                'mriqc', op.join('data', 'reports', 'cover_group.rst')))
        else:
            template = ConfigGen(pkgr.resource_filename(
                'mriqc', op.join('data', 'reports', 'cover_individual.rst')))

        RstToPdf().createPdf(
            text=template.compile(context), output=out_file)
Beispiel #18
0
 def render(self, input_file, output_file):
     style_file = os.path.join(self.base_dir, self.style)
     with open(input_file) as input_file:
         RstToPdf(stylesheets=[style_file]).createPdf(
             text=input_file.read(), output=output_file)