示例#1
0
def build_pdf_for_glossary(glossary_id, glossary_name, temp_dir):
    #
    # Some string include Unicode byte order marker ('\ufeff') to get the PDF
    # library to interpret them correctly. Without those markers non-ASCII
    # characters like German umlauts are not displyed correctly in the created
    # PDF file.
    #
    part = []
    logger.info('Loading glossary: {} - {}'.format(glossary_id, glossary_name))
    # create heading
    heading = document.pisaStory(
        '\ufeff<h1>{} (Glossar)</h1>'.format(glossary_name)).story
    part.extend(heading)
    # build paragraphs for questions
    for question, answer in moodle.get_entries_for_glossary(
            glossary_id, temp_dir):
        part.extend(
            document.pisaStory('\ufeff<h2>{}</h2>'.format(question)).story)
        bs = BeautifulSoup(answer,
                           features='html.parser')  # 'lxml', 'html5lib'
        filter_for_xhtml2pdf(bs, temp_dir)
        part.extend(document.pisaStory('\ufeff{}'.format(bs)).story)
        # insert divider between entries (see https://stackoverflow.com/a/36112136)
        part.append(HRFlowable(width='40%', thickness=2, color='darkgray'))
    # pop last divider
    part.pop()
    part.append(PageBreak())
    return part
示例#2
0
def build_pdf_for_database(database_id, database_name, temp_dir):
    part = []
    logger.info('Loading database: {} - {}'.format(database_id, database_name))
    # create heading
    heading = document.pisaStory(
        '\ufeff<h1>{} (Datenbank)</h1>'.format(database_name)).story
    part.extend(heading)
    # build paragraphs for questions
    for entry in moodle.get_entries_for_database(database_id):
        entry_heading = document.pisaStory('\ufeff<h2>Eintrag: {}</h2>'.format(
            entry['id'])).story
        part.extend(entry_heading)
        for k, v in entry.items():
            # exclude file list
            if k != 'files' and k != 'id':
                if v in entry['files']:
                    image_file_name = moodle.download_image(
                        entry['files'][v], temp_dir)
                    part.append(Image(
                        image_file_name))  # , width=width, height=height)
                else:
                    part.extend(
                        document.pisaStory('\ufeff<h3>{}</h3><p>{}</p>'.format(
                            k, v)).story)
        # bs = BeautifulSoup(page_content, features='html.parser')
        # filter_for_xhtml2pdf(bs, temp_dir)
        # part.extend(document.pisaStory('\ufeff{}'.format(bs)).story)
    part.append(PageBreak())
    return part
示例#3
0
    def parseHTML(data, node):
        path = None
        link_callback = None
        debug = 0
        default_css = HTML_CSS
        xhtml = False
        encoding = None
        xml_output = None
        capacity = 100 * 1024

        # Prepare simple context
        context = pisaContext(path, debug=debug, capacity=capacity)
        context.pathCallback = link_callback

        # Build story
        context = pisaStory(
            data,
            path,
            link_callback,
            debug,
            default_css,
            xhtml,
            encoding,
            context=context,
            xml_output=xml_output,
        )
        return context.story
示例#4
0
def draw_story_back(c, story, positions, position=0):
    c.saveState()
    pos = positions[position]
    size = SIZE
    c.translate(pos[0] * cm, pos[1] * cm)
    c.setStrokeColorRGB(0.2, 0.3, 0.5)
    c.rect(0, 0, size[0] * cm, size[1] * cm, fill=0)

    stylesheet = getSampleStyleSheet()
    normal_style = stylesheet['Normal']

    # Code
    p = Paragraph(u"<font size=15>{0}</font>".format(story.code), normal_style)
    p.wrap(5 * cm, 2 * cm)
    p.drawOn(c, 1.1 * cm, (SIZE[1] - TOP_HEIGHT + 1.5) * cm)

    # Draw acceptance criteria
    html = u"""
        <div style="font-size:13px">{0}</div>
    """.format(markdown(story.acceptances))
    context = pisaStory(html)
    f = Frame(0,
              0,
              size[0] * cm, (size[1] - TOP_HEIGHT / 2) * cm,
              showBoundary=1)
    f.addFromList(context.story, c)
    c.restoreState()
示例#5
0
def build_pdf_for_wiki(wiki_id, wiki_name, temp_dir):
    part = []
    logger.info('Loading wiki: {} - {}'.format(wiki_id, wiki_name))
    # create heading
    heading = document.pisaStory(
        '\ufeff<h1>{} (Wiki)</h1>'.format(wiki_name)).story
    part.extend(heading)
    # build paragraphs for questions
    for page_id, page_name, page_content in moodle.get_subwiki_pages(wiki_id):
        part.extend(
            document.pisaStory('\ufeff<h2>{}</h2>'.format(page_name)).story)
        bs = BeautifulSoup(page_content, features='html.parser')
        # TODO: Handle if image is external link to another site.
        filter_for_xhtml2pdf(bs, temp_dir)
        part.extend(document.pisaStory('\ufeff{}'.format(bs)).story)
    part.append(PageBreak())
    return part
示例#6
0
    def parseHTML(data, node):
        path = None
        link_callback = None
        debug = 0
        default_css = HTML_CSS
        xhtml = False
        encoding = None
        xml_output = None
        capacity = 100 * 1024

        # Prepare simple context
        context = pisaContext(path, debug=debug, capacity=capacity)
        context.pathCallback = link_callback

        # Build story
        context = pisaStory(
            data, path, link_callback, debug, default_css, xhtml, encoding, context=context, xml_output=xml_output
        )
        return context.story
示例#7
0
def draw_story_back(c, story, positions, position=0):
    c.saveState()
    pos = positions[position]
    size = SIZE
    c.translate(pos[0]*cm, pos[1]*cm)
    c.setStrokeColorRGB(0.2, 0.3, 0.5)
    c.rect(0, 0, size[0]*cm, size[1]*cm, fill=0)

    stylesheet = getSampleStyleSheet()
    normalStyle = stylesheet['Normal']

     # Code
    p = Paragraph(u"<font size=15>{0}</font>".format(story.code), normalStyle)
    p.wrap(5*cm, 2*cm)
    p.drawOn(c, 1.1*cm, (SIZE[1]-TOP_HEIGHT+1.5)*cm)

    # Draw acceptance criteria
    html = u"""
        <div style="font-size:13px">{0}</div>
    """.format(markdown(story.acceptances))
    context = pisaStory(html)
    f = Frame(0, 0, size[0]*cm, (size[1]-TOP_HEIGHT/2)*cm, showBoundary=1)
    f.addFromList(context.story, c)
    c.restoreState()