Ejemplo n.º 1
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'
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 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)
Ejemplo n.º 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])
Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 8
0
def rst2pdf(content, theme=None):
    topdf = RstToPdf(basedir=current_app.config.root_path, breaklevel=0)

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

    return buf.getvalue()
Ejemplo n.º 9
0
def pdf_output(locale, **extra_settings):
    if "styleshees" not in extra_settings:
        extra_settings["stylesheets"] = load_stylesheets("*.style")
    if "breaklevel" not in extra_settings:
        extra_settings["breaklevel"] = 0
    parser = RstToPdf(**extra_settings)
    with closing(StringIO()) as fp:
        parser.createPdf(text=split_rst_file(locale), output=fp)
        rs = fp.getvalue()
    return rs
Ejemplo n.º 10
0
            def download(self, pageid):
                from rst2pdf.createpdf import RstToPdf
                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()
Ejemplo n.º 11
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")
Ejemplo n.º 12
0
class PdfGenerator(Generator):
    """Generate PDFs on the output dir, for all articles and pages coming from
    rst"""
    def __init__(self, *args, **kwargs):
        super(PdfGenerator, self).__init__(*args, **kwargs)
        try:
            from rst2pdf.createpdf import RstToPdf
            if 'PDF_STYLE_PATH' in self.settings.keys():
                pdf_style_path = os.path.join(self.settings['PDF_STYLE_PATH'])
            else:
                pdf_style_path = ''

            if 'PDF_STYLE' in self.settings.keys():
                pdf_style = self.settings.get('PDF_STYLE', 'twelvepoint')

            self.pdfcreator = RstToPdf(breakside=0,
                                       stylesheets=[pdf_style],
                                       style_path=[pdf_style_path])
        except ImportError:
            raise Exception("unable to find rst2pdf")

    def _create_pdf(self, obj, output_path):
        if obj.source_path.endswith('.rst'):
            filename = obj.slug + ".pdf"
            output_pdf = os.path.join(output_path, filename)
            # print('Generating pdf for', obj.source_path, 'in', output_pdf)
            with open(obj.source_path) as f:
                self.pdfcreator.createPdf(text=f.read(), output=output_pdf)
            logger.info(' [ok] writing %s' % output_pdf)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(' Generating PDF files...')
        pdf_path = os.path.join(self.output_path, 'pdf')
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " +
                             pdf_path)

        for article in self.context['articles']:
            self._create_pdf(article, pdf_path)

        for page in self.context['pages']:
            self._create_pdf(page, pdf_path)
Ejemplo n.º 13
0
class PdfGenerator(Generator):
    """Generate PDFs on the output dir, for all articles and pages coming from
    rst"""
    def __init__(self, *args, **kwargs):
        super(PdfGenerator, self).__init__(*args, **kwargs)
        try:
            from rst2pdf.createpdf import RstToPdf
            if 'PDF_STYLE_PATH' in self.settings.keys():
                pdf_style_path = os.path.join(self.settings['PDF_STYLE_PATH'])
            else:
                pdf_style_path = ''

            if 'PDF_STYLE' in self.settings.keys():
                pdf_style = self.settings.get('PDF_STYLE', 'twelvepoint')

            self.pdfcreator = RstToPdf(breakside=0,
                                       stylesheets=[pdf_style],
                                       style_path=[pdf_style_path])
        except ImportError:
            raise Exception("unable to find rst2pdf")

    def _create_pdf(self, obj, output_path):
        if obj.source_path.endswith('.rst'):
            filename = obj.slug + ".pdf"
            output_pdf = os.path.join(output_path, filename)
            # print('Generating pdf for', obj.source_path, 'in', output_pdf)
            with open(obj.source_path) as f:
                self.pdfcreator.createPdf(text=f.read(), output=output_pdf)
            logger.info(' [ok] writing %s' % output_pdf)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(' Generating PDF files...')
        pdf_path = os.path.join(self.output_path, 'pdf')
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " +
                             pdf_path)

        for article in self.context['articles']:
            self._create_pdf(article, pdf_path)

        for page in self.context['pages']:
            self._create_pdf(page, pdf_path)
Ejemplo n.º 14
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()
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
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)
Ejemplo n.º 17
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()
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
class PdfGenerator(Generator):
    """Generate PDFs on the output dir, for all articles and pages coming from
    rst"""

    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"]) if "PDF_STYLE_PATH" in self.settings.keys() else ""
            )
            pdf_style = self.settings["PDF_STYLE"] if "PDF_STYLE" in self.settings.keys() else "twelvepoint"
            self.pdfcreator = RstToPdf(breakside=0, stylesheets=[pdf_style], style_path=[pdf_style_path])
        except ImportError:
            raise Exception("unable to find rst2pdf")

    def _create_pdf(self, obj, output_path):
        if obj.filename.endswith(".rst"):
            filename = obj.slug + ".pdf"
            output_pdf = os.path.join(output_path, filename)
            # print "Generating pdf for", obj.filename, " in ", output_pdf
            with open(obj.filename) as f:
                self.pdfcreator.createPdf(text=f.read(), output=output_pdf)
            logger.info(" [ok] writing %s" % output_pdf)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(" Generating PDF files...")
        pdf_path = os.path.join(self.output_path, "pdf")
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " + pdf_path)

        for article in self.context["articles"]:
            self._create_pdf(article, pdf_path)

        for page in self.context["pages"]:
            self._create_pdf(page, pdf_path)
Ejemplo n.º 21
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
Ejemplo n.º 22
0
class PdfGenerator(Generator):
    """Generate PDFs on the output dir, for all articles and pages coming from
    rst"""
    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)

    def _create_pdf(self, obj, output_path):
        if obj.filename.endswith(".rst"):
            filename = obj.slug + ".pdf"
            output_pdf = os.path.join(output_path, filename)
            # print "Generating pdf for", obj.filename, " in ", output_pdf
            with open(obj.filename) as f:
                self.pdfcreator.createPdf(text=f, output=output_pdf)
            logger.info(u' [ok] writing %s' % output_pdf)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(u' Generating PDF files...')
        pdf_path = os.path.join(self.output_path, 'pdf')
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " +
                             pdf_path)
                pass

        for article in self.context['articles']:
            self._create_pdf(article, pdf_path)

        for page in self.context['pages']:
            self._create_pdf(page, pdf_path)
Ejemplo n.º 23
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")
Ejemplo n.º 24
0
class PdfGenerator(Generator):
    """Generate PDFs on the output dir, for all articles and pages coming from
    rst"""
    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)

    def _create_pdf(self, obj, output_path):
        if obj.filename.endswith(".rst"):
            filename = obj.slug + ".pdf"
            output_pdf = os.path.join(output_path, filename)
            # print "Generating pdf for", obj.filename, " in ", output_pdf
            with open(obj.filename) as f:
                self.pdfcreator.createPdf(text=f, output=output_pdf)
            logger.info(u' [ok] writing %s' % output_pdf)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(u' Generating PDF files...')
        pdf_path = os.path.join(self.output_path, 'pdf')
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " + pdf_path)
                pass

        for article in self.context['articles']:
            self._create_pdf(article, pdf_path)

        for page in self.context['pages']:
            self._create_pdf(page, pdf_path)
Ejemplo n.º 25
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"]) if "PDF_STYLE_PATH" in self.settings.keys() else ""
            )
            pdf_style = self.settings["PDF_STYLE"] if "PDF_STYLE" in self.settings.keys() else "twelvepoint"
            self.pdfcreator = RstToPdf(breakside=0, stylesheets=[pdf_style], style_path=[pdf_style_path])
        except ImportError:
            raise Exception("unable to find rst2pdf")
Ejemplo n.º 26
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
Ejemplo n.º 27
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
Ejemplo n.º 28
0
    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)
Ejemplo n.º 29
0
def document(report, fmt='html'):
    '''
    Creates a document in the desired `fmt`. Defaults to html.
    '''

    if fmt == 'csv':
        return report2csv(report)

    doctree = doctree_factory(report, fmt=fmt)

    dt_settings = {
        'input_encoding': 'unicode',
    }

    if fmt == 'pdf':
        if not RST2PDF:
            raise Exception('rst2pdf is not available')
            return None
        buff = StringIO()
        rst2pdf = RstToPdf()
        rst2pdf.createPdf(doctree=doctree, output=buff)
        return buff.getvalue()

    if  fmt in DOCUTILS_WRITER_NAMES:
        if fmt == 'odt':
            fmt = 'odf_odt'  # Docutils writer name
        if fmt in ('txt', 'rst'):
            return gen_rst(doctree, 0)
        return publish_from_doctree(
            doctree,
            writer_name=fmt,
            settings_overrides=dt_settings
        )
    else:
        raise Exception('Format not supported. Not in %s' % (
                SUPPORTED_FORMATS,
            )
        )
Ejemplo n.º 30
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)
Ejemplo n.º 31
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)
Ejemplo n.º 32
0
Archivo: pdf.py Proyecto: wombelix/pdf
class PdfGenerator(Generator):
    "Generate PDFs on the output dir, for all articles and pages"

    supported_md_fields = ["date"]

    def __init__(self, *args, **kwargs):
        super().__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)

    def _create_pdf(self, obj, output_path):
        filename = obj.slug + ".pdf"
        output_pdf = os.path.join(output_path, filename)
        mdreader = MarkdownReader(self.settings)
        _, ext = os.path.splitext(obj.source_path)

        if ext == ".rst":
            with open(obj.source_path, encoding="utf-8") as f:
                text = f.read()

            header = ""
        elif ext[1:] in mdreader.file_extensions and mdreader.enabled:
            text, meta = mdreader.read(obj.source_path)
            header = ""

            if "title" in meta:
                title = meta["title"]
                header = title + "\n" + "#" * len(title) + "\n\n"
                del meta["title"]

            for k in list(meta):
                # We can't support all fields, so we strip the ones that won't
                # look good
                if k not in self.supported_md_fields:
                    del meta[k]

            header += "\n".join([":{}: {}".format(k, meta[k]) for k in meta])
            header += "\n\n.. raw:: html\n\n\t"
            text = text.replace("\n", "\n\t")

            # rst2pdf casts the text to str and will break if it finds
            # non-escaped characters. Here we nicely escape them to XML/HTML
            # entities before proceeding
            text = text.encode("ascii", "xmlcharrefreplace").decode()
        else:
            # We don't support this format
            logger.warn("Ignoring unsupported file " + obj.source_path)
            return

        # Find intra-site links and replace placeholder with actual path / url
        hrefs = self._get_intrasite_link_regex()
        text = hrefs.sub(lambda m: obj._link_replacer(obj.get_siteurl(), m),
                         text)

        logger.info(" [ok] writing %s" % output_pdf)

        self.pdfcreator.createPdf(text=(header + text), output=output_pdf)

    def _get_intrasite_link_regex(self):
        intrasite_link_regex = self.settings["INTRASITE_LINK_REGEX"]
        regex = r"""
                (?P<markup>)(?P<quote>)(?P<path>(\:?){}(?P<value>.*?)(?=[>\n]))
                """.format(intrasite_link_regex)
        return re.compile(regex, re.X)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(" Generating PDF files...")
        pdf_path = os.path.join(self.output_path, "pdf")
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " +
                             pdf_path)

        for obj in chain(self.context["articles"], self.context["pages"]):
            self._create_pdf(obj, pdf_path)
            for obj_trans in obj.translations:
                self._create_pdf(obj_trans, pdf_path)
Ejemplo n.º 33
0
 def setUp(self):
     self.converter=RstToPdf()
Ejemplo n.º 34
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)
Ejemplo n.º 35
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)
Ejemplo n.º 36
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)
Ejemplo n.º 37
0
class PdfGenerator(Generator):
    "Generate PDFs on the output dir, for all articles and pages"

    supported_md_fields = ["date"]

    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)

    def _create_pdf(self, obj, output_path):
        filename = obj.slug + ".pdf"
        output_pdf = os.path.join(output_path, filename)
        mdreader = MarkdownReader(self.settings)
        _, ext = os.path.splitext(obj.source_path)
        if ext == ".rst":
            with open(obj.source_path, encoding="utf-8") as f:
                text = f.read()
            header = ""
        elif ext[1:] in mdreader.file_extensions and mdreader.enabled:
            text, meta = mdreader.read(obj.source_path)
            header = ""

            if "title" in meta:
                title = meta["title"]
                header = title + "\n" + "#" * len(title) + "\n\n"
                del meta["title"]

            for k in meta.keys():
                # We can't support all fields, so we strip the ones that won't
                # look good
                if k not in self.supported_md_fields:
                    del meta[k]

            header += "\n".join([":%s: %s" % (k, meta[k]) for k in meta])
            header += "\n\n.. raw:: html\n\n\t"
            text = text.replace("\n", "\n\t")

            # rst2pdf casts the text to str and will break if it finds
            # non-escaped characters. Here we nicely escape them to XML/HTML
            # entities before proceeding
            text = text.encode("ascii", "xmlcharrefreplace").decode()
        else:
            # We don't support this format
            logger.warn("Ignoring unsupported file " + obj.source_path)
            return

        logger.info(" [ok] writing %s" % output_pdf)
        self.pdfcreator.createPdf(text=(header + text), output=output_pdf)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(" Generating PDF files...")
        pdf_path = os.path.join(self.output_path, "pdf")
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " +
                             pdf_path)

        for article in self.context["articles"]:
            self._create_pdf(article, pdf_path)

        for page in self.context["pages"]:
            self._create_pdf(page, pdf_path)
Ejemplo n.º 38
0
class PdfGenerator(Generator):
    "Generate PDFs on the output dir, for all articles and pages"

    supported_md_fields = ['date']

    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)

    def _create_pdf(self, obj, output_path):
        filename = obj.slug + '.pdf'
        output_pdf = os.path.join(output_path, filename)
        mdreader = MarkdownReader(self.settings)
        _, ext = os.path.splitext(obj.source_path)
        if ext == '.rst':
            with open(obj.source_path, encoding='utf-8') as f:
                text = f.read()
            header = ''
        elif ext[1:] in mdreader.file_extensions and mdreader.enabled:
            text, meta = mdreader.read(obj.source_path)
            header = ''

            if 'title' in meta:
                title = meta['title']
                header = title + '\n' + '#' * len(title) + '\n\n'
                del meta['title']

            for k in meta.keys():
                # We can't support all fields, so we strip the ones that won't
                # look good
                if k not in self.supported_md_fields:
                    del meta[k]

            header += '\n'.join([':%s: %s' % (k, meta[k]) for k in meta])
            header += '\n\n.. raw:: html\n\n\t'
            text = text.replace('\n', '\n\t')

            # rst2pdf casts the text to str and will break if it finds
            # non-escaped characters. Here we nicely escape them to XML/HTML
            # entities before proceeding
            text = text.encode('ascii', 'xmlcharrefreplace')
        else:
            # We don't support this format
            logger.warn('Ignoring unsupported file ' + obj.source_path)
            return

        logger.info(' [ok] writing %s' % output_pdf)
        self.pdfcreator.createPdf(text=(header+text),
                                  output=output_pdf)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(' Generating PDF files...')
        pdf_path = os.path.join(self.output_path, 'pdf')
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " +
                             pdf_path)

        for article in self.context['articles']:
            self._create_pdf(article, pdf_path)

        for page in self.context['pages']:
            self._create_pdf(page, pdf_path)
Ejemplo n.º 39
0
class PdfGenerator(Generator):
    """Generate PDFs on the output dir, for all articles and pages coming from
    rst"""
    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],
                                   def_dpi=100)

    def _create_pdf(self, obj, output_path):
        if obj.source_path.endswith('.rst'):
            filename = obj.slug + ".pdf"
            output_pdf = os.path.join(output_path, filename)
            print('Generating pdf for', obj.source_path, 'in', output_pdf)
            with open(obj.source_path) as f:
                self.pdfcreator.createPdf(text=f.read(), output=output_pdf)
            logger.info(' [ok] writing %s' % output_pdf)

    def _upload_figshare(self, obj, output_path):
        if obj.source_path.endswith('.rst'):
            filename = obj.slug + ".pdf"
            output_pdf = os.path.join(output_path, filename)
            client_key = '8BeN60jNpgmgIN6G8oaCXQ'
            client_secret = 'oSj6EZkWChEifWOADJcKUw'
            token_key = 'sZegoc5oOgPsXfeATZGvRggVLkqc9lrYFCI9pYvDWkZAsZegoc5oOgPsXfeATZGvRg'
            token_secret = 'fifOafUWFAFcKxN6ZO9fbg'
            oauth = OAuth1(client_key=client_key, client_secret=client_secret,
                           resource_owner_key=token_key, resource_owner_secret=token_secret,
                                          signature_type = 'auth_header')
            client = requests.session()
            body = {'title':obj.title, 'description':obj.summary,'defined_type':'dataset'}
            headers = {'content-type':'application/json'}

            #response = client.post('http://api.figshare.com/v1/my_data/articles', auth=oauth, data=json.dumps(body), headers=headers)

            #results = json.loads(response.content)
            #print(results["doi"])
            #article_id = results["article_id"]
            article_id = 852126

            body = {'category_id':77} # applied computer science
            headers = {'content-type':'application/json'}
            response = client.put('http://api.figshare.com/v1/my_data/articles/%d/categories' % article_id, auth=oauth,
                                    data=json.dumps(body), headers=headers)
            results = json.loads(response.content)

            body = {'tag_name':'proceedings'}
            headers = {'content-type':'application/json'}
            response = client.put('http://api.figshare.com/v1/my_data/articles/%d/tags' % article_id, auth=oauth,
                                    data=json.dumps(body), headers=headers)
            results = json.loads(response.content)
            authors = [author.strip() for author in obj.author.name.split(",")]
            for author in authors:
                print(author)

                response = client.get('http://api.figshare.com/v1/my_data/authors?search_for=Kapil Arya', auth=oauth)
                results = json.loads(response.content)
                print(results)

                if results["results"] == 0:
                    body = {'full_name':author}
                    headers = {'content-type':'application/json'}
                    response = client.post('http://api.figshare.com/v1/my_data/authors', auth=oauth,
                                            data=json.dumps(body), headers=headers)
                    results = json.loads(response.content)
                    print(results)

                body = {'author_id':results["author_id"]}
                headers = {'content-type':'application/json'}

                response = client.put('http://api.figshare.com/v1/my_data/articles/%d/authors' % article_id, auth=oauth,
                                        data=json.dumps(body), headers=headers)
                results = json.loads(response.content)

                files = {'filedata':(os.path.basename(output_pdf), open(output_pdf, 'rb'))}

                response = client.put('http://api.figshare.com/v1/my_data/articles/%d/files' % article_id, auth=oauth,
                                      files=files)
                results = json.loads(response.content)
                print(results)
                #response = client.post('http://api.figshare.com/v1/my_data/articles/%d/action/make_public' % article_id, auth=oauth)
                #results = json.loads(response.content)

    def generate_context(self):
        pass

    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        logger.info(' Generating PDF files...')
        pdf_path = os.path.join(self.output_path, 'pdf')
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                logger.error("Couldn't create the pdf output folder in " +
                             pdf_path)

        for article in self.context['articles']:
            self._create_pdf(article, pdf_path)
            self._upload_figshare(article, pdf_path)

        for page in self.context['pages']:
            self._create_pdf(page, pdf_path)