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)
Example #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'
Example #3
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()
Example #4
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()
Example #5
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
Example #6
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()
Example #7
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()
Example #8
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)
Example #9
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)
Example #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()
Example #11
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)
Example #12
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)
Example #13
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()
Example #14
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)
Example #15
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
Example #16
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)
Example #17
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)
Example #18
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
Example #19
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
Example #20
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)
Example #21
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,
            )
        )
Example #22
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)
Example #23
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)
Example #24
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)
Example #25
0
File: pdf.py Project: 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)