Ejemplo n.º 1
1
    def test_persian_in_pdf_language(self):
        """
            Tests if 'persian' is the value of the 'language' attribute.
        """
        text = 'سلام. من 3 فرزند و 10 گربه دارم. عالی است!'
        language_tag = '<pdf:language name="persian"/>'

        html = self.HTML_CONTENT.format(ff_R=self.ff_R,
                                        ff_M=self.ff_M,
                                        ff_B=self.ff_B,
                                        ff_SB=self.ff_SB,
                                        ff_V=self.ff_V,
                                        text=text,
                                        language_tag=language_tag)

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=html, dest=pdf_file)

            self.assertEqual(pisa_doc.language, 'persian',
                             '"persian" not detected in <pdf:language>!')
Ejemplo n.º 2
0
def markdown_to_pdf(markdown_path: Path):
    html_text = markdown(markdown_path.read_text(),
                         output_format="html",
                         extensions=["markdown.extensions.tables"])
    html_text += """<style>
    td, th { 
        border: 1px solid #666666; 
        text-align:center;
    }
    td, th {
        padding-top:4px;
    }
    tr:nth-child(odd) {
        background-color: red;
    }
    th {
        width: 50%;
    }
    </style>"""
    for b in ["td", "th"]:
        for p in ["left", "right"]:
            html_text = html_text.replace(f"""<{b} align="{p}">""", "<td>")
    with markdown_path.with_suffix(".pdf").open("wb") as f, working_directory(
            markdown_path.parent):
        pisaDocument(html_text, dest=f)
Ejemplo n.º 3
0
def test_document_creation_with_css_metadata():
    for css_code in CSS_TESTS:
        with tempfile.TemporaryFile() as pdf_file:
            pisaDocument(src=io.StringIO(HTML_CONTENT.format(head=css_code)),
                         dest=pdf_file,
                         context_meta=METADATA)
            _compare_pdf_metadata(pdf_file, tools.assert_equal)
Ejemplo n.º 4
0
 def test_document_creation_with_metadata(self):
     with tempfile.TemporaryFile() as pdf_file:
         pisaDocument(src=io.StringIO(
             HTML_CONTENT.format(head="", extra_html="")),
                      dest=pdf_file,
                      context_meta=METADATA)
         self._compare_pdf_metadata(pdf_file, self.assertEqual)
Ejemplo n.º 5
0
    def test_document_background_image(self):
        """
        Test that a transparent PNG image is rendered properly.
        """
        tests_folder = os.path.dirname(os.path.realpath(__file__))
        image_path = os.path.join(tests_folder, 'samples', 'img',
                                  'denker-transparent.png')

        css_background = """<style>@page {{background-image: url('{background_location}');
                         @frame {{left: 10pt}}}}</style>""".format(
            background_location=image_path)

        with tempfile.TemporaryFile() as pdf_file:
            pisaDocument(src=io.StringIO(
                HTML_CONTENT.format(head=css_background, extra_html="")),
                         dest=pdf_file)
            pdf_file.seek(0)
            pdf_reader = PdfFileReader(pdf_file)

            xobjects = pdf_reader.getPage(
                0)['/Resources']['/XObject'].getObject()
            objects = [xobjects[key] for key in xobjects.keys()]

            # Identity the 'denker_transparent.png' image by its height and width, and make sure it's there.
            denker_transparant = [
                obj for obj in objects
                if obj['/Height'] == 137 and obj['/Width'] == 70
            ]
            self.assertEqual(len(denker_transparant), 1)
Ejemplo n.º 6
0
def test_document_creation_without_metadata():
    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT.format(head="", extra_html="")),
            dest=pdf_file
        )
        _compare_pdf_metadata(pdf_file, tools.assert_not_equal)
Ejemplo n.º 7
0
def test_document_creation_without_metadata():
    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT),
            dest=pdf_file
        )
        _compare_pdf_metadata(pdf_file, tools.assert_not_equal)
Ejemplo n.º 8
0
    def test_document_with_transparent_image(self):
        """
        Test that a transparent PNG image is rendered properly.
        """
        tests_folder = os.path.dirname(os.path.realpath(__file__))
        image_path = os.path.join(tests_folder, 'samples', 'img',
                                  'denker-transparent.png')
        extra_html = "<img src=\"{image_path}\">".format(image_path=image_path)

        with tempfile.TemporaryFile() as pdf_file:
            pisaDocument(src=io.StringIO(
                HTML_CONTENT.format(head="", extra_html=extra_html)),
                         dest=pdf_file)
            pdf_file.seek(0)
            pdf_reader = PdfFileReader(pdf_file)

            xobjects = pdf_reader.getPage(
                0)['/Resources']['/XObject'].getObject()
            objects = [xobjects[key] for key in xobjects.keys()]

            # Identity the 'denker_transparent.png' image by its height and width, and make sure it's there.
            denker_transparant = [
                obj for obj in objects
                if obj['/Height'] == 137 and obj['/Width'] == 70
            ]
            self.assertEqual(len(denker_transparant), 1)
def test_document_creation_with_metadata():
    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT),
            dest=pdf_file,
            context_meta=METADATA
        )
        _compare_pdf_metadata(pdf_file, tools.assert_equal)
Ejemplo n.º 10
0
def test_in_memory_document():
    with io.BytesIO() as in_memory_file:
        pisaDocument(HTML_CONTENT.format(head="", extra_html=""), dest=in_memory_file)
        tools.assert_greater(len(in_memory_file.getvalue()), 0)

    with io.BytesIO() as in_memory_file:
        pisaDocument(io.StringIO(HTML_CONTENT.format(head="", extra_html="")), dest=in_memory_file)
        tools.assert_greater(len(in_memory_file.getvalue()), 0)
Ejemplo n.º 11
0
def test_document_creation_with_metadata():
    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT.format(head="", extra_html="")),
            dest=pdf_file,
            context_meta=METADATA
        )
        _compare_pdf_metadata(pdf_file, tools.assert_equal)
Ejemplo n.º 12
0
def test_in_memory_document():
    with io.BytesIO() as in_memory_file:
        pisaDocument(HTML_CONTENT.format(head="", extra_html=""), dest=in_memory_file)
        tools.assert_greater(len(in_memory_file.getvalue()), 0)

    with io.BytesIO() as in_memory_file:
        pisaDocument(io.StringIO(HTML_CONTENT.format(head="", extra_html="")), dest=in_memory_file)
        tools.assert_greater(len(in_memory_file.getvalue()), 0)
Ejemplo n.º 13
0
def test_document_creation_with_css_metadata():
    for css_code in CSS_TESTS:
        with tempfile.TemporaryFile() as pdf_file:
            pisaDocument(
                src=io.StringIO(HTML_CONTENT.format(head=css_code)),
                dest=pdf_file,
                context_meta=METADATA
            )
            _compare_pdf_metadata(pdf_file, tools.assert_equal)
Ejemplo n.º 14
0
def html2pdf(html_filename, output_filename=None, **options):
    """ Convert a HTML file to PDF using FOP"""

    if not output_filename:
        output_filename = newTempfile(suffix='.pdf')

    fin = file(html_filename)
    fout = file(output_filename, 'wb')
    pisaDocument(fin, fout, encoding='utf-8', debug=True)

    fin.close()
    fout.close()
    return dict(output_filename=output_filename, status=0, output='')
Ejemplo n.º 15
0
def report(type, exercises, with_solutions=True, base_url='http://localhost'):
    '''
    render pdf/rtf/txt report
    '''
    now = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    text = get_template('report/%s.html' % type).render({
        'exercises': exercises,
        'with_solutions': with_solutions,
        'now': now,
        'base_url': base_url,
    })

    if type == 'pdf':
        fp = StringIO()
        try:
            pdf = pisaDocument(latex2img(text), fp, link_callback=_upfile_path)
            return fp.getvalue()
        except:
            pass
        finally:
            fp.close()
    elif type == 'rtf':
        convetor = HTML2RTF()
        convetor.feed(latex2img(text))
        return convetor.get_rtf()
    else:
        text = html_entity_decode(text)
        return html2text(text)
    def test_asian_font_int_pdf(self):
        """
            This function check if asian fonts used in font-family are in
            the pdf result.

            ASIAN_FONT_LIST_USED a dict that contain all fonts used in the style
            real_asian_font a dict that contain all fonts in the pdf result
            (validated through assertDictEqual() have to be equals)
        """

        ASIAN_FONT_LIST_USED = {
            'heiseimin-w3': 'HeiseiMin-W3',
            'hysmyeongjo-medium': 'HYSMyeongJo-Medium'
        }

        html = self.HTML_CONTENT
        real_asian_font = {}
        result = BytesIO()
        pdf = pisaDocument(BytesIO(html.encode('utf-8')), result)

        for font in ASIAN_FONT_LIST_USED:
            font_value = ASIAN_FONT_LIST_USED.get(font)
            if font_value == pdf.getFontName(font):
                real_asian_font[font] = font_value

        self.assertDictEqual(ASIAN_FONT_LIST_USED, real_asian_font)
Ejemplo n.º 17
0
def generate_pdf(filename, uid, status_ids, with_head=True, capacity=50*1024):

    #########Set FONT################
    from xhtml2pdf.default import DEFAULT_FONT
    from xhtml2pdf.document import pisaDocument
    from reportlab.pdfbase import pdfmetrics
    from reportlab.pdfbase.ttfonts import TTFont

    pdfmetrics.registerFont(TTFont('zhfont', os.path.join(app.root_path, 'static/font/yahei-consolas.ttf')))
    DEFAULT_FONT["helvetica"] = "zhfont"
    css = open(os.path.join(app.root_path, "static/css/pdf.css")).read()

    #result = StringIO.StringIO()
    full_file_name = get_pdf_full_filename(filename)
    if not full_file_name:
        return None
    result = open(full_file_name, 'wb', 1024*1000)

    user = User.get(uid)
    if not user:
        return None

    # get status
    status_list = Status.gets(status_ids)
    _html = render(user, status_list, with_head)
    _pdf = pisaDocument(_html, result, default_css=css, link_callback=link_callback, capacity=capacity)
    result.close()

    if not _pdf.err:
        return full_file_name
    else:
        return None
Ejemplo n.º 18
0
def rechnung_drucken(request, pk):
    rechnung = get_object_or_404(Rechnung, pk=pk)
    context = {
        "rechnung": rechnung,
        "is_copy": "is_copy" in request.GET,
    }

    html = render_to_string("pyrm/html_print/rechnung.html", context,
        context_instance=RequestContext(request)
    )

    if "html" in request.GET:
        return HttpResponse(html)

    result = StringIO()

    #log = logging.getLogger("xhtml2pdf")
    pdf = pisaDocument(
        StringIO(html.encode("UTF-8")),
        dest=result,
        link_callback=fetch_resources
    )
    if pdf.err:
        response = HttpResponse(pdf.err, content_type="text/plain")
    else:
        response = HttpResponse(result.getvalue(), mimetype='application/pdf')
        filename = "rechnung%s.pdf" % rechnung.nummer
        response['Content-Disposition'] = 'attachment; filename=%s' % filename

    return response
Ejemplo n.º 19
0
def generate_pdf(filename, uid, start, count, cate=None, with_head=True, capacity=50*1024):

    #########Set FONT################
    from xhtml2pdf.default import DEFAULT_FONT
    from xhtml2pdf.document import pisaDocument
    from reportlab.pdfbase import pdfmetrics
    from reportlab.pdfbase.ttfonts import TTFont

    pdfmetrics.registerFont(TTFont('zhfont', os.path.join(app.root_path, 'static/font/yahei-consolas.ttf')))
    DEFAULT_FONT["helvetica"] = "zhfont"
    css = open(os.path.join(app.root_path, "static/css/pdf.css")).read()

    #result = StringIO.StringIO()
    full_file_name = get_pdf_full_filename(filename)
    if not full_file_name:
        return None
    result = open(full_file_name, 'wb', 1024*1000)

    user = User.get(uid)
    if not user:
        return None

    # get status
    ids = Status.get_ids(user_id=uid, start=start, limit=count, cate=cate)
    status_list = Status.gets(ids)
    _html = render(user, status_list, with_head)
    _pdf = pisaDocument(_html, result, default_css=css, link_callback=link_callback, capacity=capacity)
    result.close()

    if not _pdf.err:
        return full_file_name
    else:
        return None
Ejemplo n.º 20
0
 def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
     package = self.pool.get('stock.packages').browse(cr, uid, ids[0], context=context)
     report_content = ''
     control_receipt = package.control_log_receipt
     if control_receipt:
         control_receipt = base64.decodestring(control_receipt)
         from xhtml2pdf.default import DEFAULT_CSS
         from xhtml2pdf.document import pisaDocument
         import sys
         import tempfile
         CreatePDF = pisaDocument
         path = tempfile.mktemp() + '.html'
         temp = file(path, 'wb')
         temp.write(control_receipt)
         temp.close()
         fsrc = open(path, 'rb')
         dest_file = tempfile.mktemp() + '.pdf'
         fdest = open(dest_file, 'wb')
         pdf = pisaDocument(fsrc, fdest, debug=0, path = path, errout = sys.stdout,
                            tempdir = None, format = 'pdf', link_callback = None,
                            default_css = None, xhtml = False, encoding = None, xml_output = None)
         fdest.close()
         out_file = open(dest_file, 'rb')
         report_content = out_file.read()
         out_file.close()
         
     report_xml.report_type = "pdf"        
     return (report_content, report_xml.report_type)
 def setUp(self):
     #Setting values that to be used in the following methods
     html = self.HTML_CONTENT.format(FFBold=self.FFBold,
                                     FFRegular=self.FFRegular,
                                     fRegular=self.fRegular,
                                     fBold=self.fBold)
     with io.BytesIO() as pdf_file:
         self.pisa_doc = pisaDocument(src=html, dest=pdf_file)
Ejemplo n.º 22
0
def Create_PDF_into_buffer(html, font_file_path):

    # # 注册字体
    pdfmetrics.registerFont(TTFont('yahei', font_file_path))
    #
    fonts.addMapping('song', 0, 0, 'song')
    fonts.addMapping('song', 0, 1, 'song')
    DEFAULT_FONT['helvetica'] = 'yahei'
    xhtml2pdf.reportlab_paragraph.Paragraph.wrap = wrap
    return pisaDocument(html)
Ejemplo n.º 23
0
def test_document_background_image_not_on_all_pages():
    """
    Test that all pages are being rendered, when background is a pdf file and it's applied for the first page only.
    """
    tests_folder = os.path.dirname(os.path.realpath(__file__))
    background_path = os.path.join(tests_folder, 'samples', 'images.pdf')

    css = "<style>@page {{background-image: url({background_location});}} @page two {{}}</style>".format(
        background_location=background_path)

    extra_html = """<pdf:nexttemplate name="two"> <pdf:nextpage> <p>Hello, world!</p>"""

    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(src=io.StringIO(
            HTML_CONTENT.format(head=css, extra_html=extra_html)),
                     dest=pdf_file)
        pdf_file.seek(0)
        pdf_reader = PdfFileReader(pdf_file)

        tools.assert_equal(pdf_reader.getNumPages(), 2)
Ejemplo n.º 24
0
def test_document_background_image_not_on_all_pages():
    """
    Test that all pages are being rendered, when background is a pdf file and it's applied for the first page only.
    """
    tests_folder = os.path.dirname(os.path.realpath(__file__))
    background_path = os.path.join(tests_folder, 'samples', 'images.pdf')

    css = "<style>@page {{background-image: url({background_location});}} @page two {{}}</style>".format(
        background_location=background_path)

    extra_html = """<pdf:nexttemplate name="two"> <pdf:nextpage> <p>Hello, world!</p>"""

    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT.format(head=css, extra_html=extra_html)),
            dest=pdf_file
        )
        pdf_file.seek(0)
        pdf_reader = PdfFileReader(pdf_file)

        tools.assert_equal(pdf_reader.getNumPages(), 2)
Ejemplo n.º 25
0
def test_document_with_transparent_image():
    """
    Test that a transparent PNG image is rendered properly.
    """
    tests_folder = os.path.dirname(os.path.realpath(__file__))
    image_path = os.path.join(tests_folder, 'samples', 'img', 'denker-transparent.png')
    extra_html = "<img src=\"{image_path}\">".format(image_path=image_path)

    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT.format(head="", extra_html=extra_html)),
            dest=pdf_file
        )
        pdf_file.seek(0)
        pdf_reader = PdfFileReader(pdf_file)

        xobjects = pdf_reader.getPage(0)['/Resources']['/XObject'].getObject()
        objects = [xobjects[key] for key in xobjects.keys()]

        # Identity the 'denker_transparent.png' image by its height and width, and make sure it's there.
        denker_transparant = [obj for obj in objects if obj['/Height'] == 137 and obj['/Width'] == 70]
        tools.assert_equal(len(denker_transparant), 1)
Ejemplo n.º 26
0
    def test_arabic_check_language_in_pdf(self):
        """
            this function is used to check if the "attr language" is
            is located in the pdf result.
        """
        html = self.HTML_CONTENT
        res = False
        result = BytesIO()
        pdf = pisaDocument(BytesIO(html.encode('utf-8')), result)

        if hasattr(pdf,'language'):
            res = True
        self.assertTrue(res)
Ejemplo n.º 27
0
    def write(self, sheet, file):
        """ Writes data from sheet into file.
        """

        table = cStringIO.StringIO()
        self.xhtml_writer(sheet, table)
        html_source = DOCUMENT_TEMPLATE.format(
                title=self.title,
                header=self.header,
                footer=self.footer,
                content=table.getvalue().decode('utf-8'))
        pdf = pisaDocument(
                html_source.encode('utf-8'), file, encoding='utf-8',
                default_css=self.css)
Ejemplo n.º 28
0
def test_th_has_no_css_rules():
    html = HTML_CONTENT

    result = BytesIO()
    pdf = pisaDocument(BytesIO(html.encode('utf-8')), result)

    parser = html5lib.HTMLParser(tree=html5lib.treebuilders.getTreeBuilder("dom"))
    document = parser.parse(html)
    th_element = document.getElementsByTagName("th")[0]
    th_element = CSSDOMElementInterface(th_element)
    attr_name = "background-color"
    rules = pdf.cssCascade.findCSSRulesFor(th_element, attr_name)

    tools.assert_list_equal(rules, [])
Ejemplo n.º 29
0
def render_to_pdf(template_src, context_dict):
    '''
    Renderiza el template con el contexto.
    Envía al cliente la Respuesta HTTP del contenido PDF para
    el template renderizado.
    '''
    
    template = get_template(template_src)
    context = Context(context_dict)
    html = template.render(context)
    result = StringIO.StringIO()
    #pdf = pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    pdf = pisaDocument(StringIO.StringIO(html.encode("utf-8")), result, encoding="utf-8")
    
    if not pdf.err:
        return HttpResponse(result.getvalue(), mimetype='application/pdf')
    
    return HttpResponse('We had some errors<pre>%s</pre>' % cgi.escape(html))
Ejemplo n.º 30
0
    def test_hebrew_in_pdf_language(self):
        """
            Tests if 'hebrew' is the value of the 'language' attribute.
        """
        text = 'שלום. יש לי 3 ילדים ו -10 חתולים. זה מגניב!'
        language_tag = '<pdf:language name="hebrew"/>'

        html = self.HTML_CONTENT.format(ff_R=self.ff_R,
                                        ff_M=self.ff_M,
                                        ff_B=self.ff_B,
                                        ff_SB=self.ff_SB,
                                        ff_V=self.ff_V,
                                        text=text,
                                        language_tag=language_tag)

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=html, dest=pdf_file)

            self.assertEqual(pisa_doc.language, 'hebrew',
                             '"hebrew" not detected in <pdf:language>!')
Ejemplo n.º 31
0
    def test_sindhi_in_pdf_language(self):
        """
            Tests if 'sindhi' is the value of the 'language' attribute.
        """
        text = 'سلام. مون وٽ 3 ٻار ۽ 10 ٻچا آهن. اهو خوفناڪ آهي!'
        language_tag = '<pdf:language name="sindhi"/>'

        html = self.HTML_CONTENT.format(ff_R=self.ff_R,
                                        ff_M=self.ff_M,
                                        ff_B=self.ff_B,
                                        ff_SB=self.ff_SB,
                                        ff_V=self.ff_V,
                                        text=text,
                                        language_tag=language_tag)

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=html, dest=pdf_file)

            self.assertEqual(pisa_doc.language, 'sindhi',
                             '"sindhi" not detected in <pdf:language>!')
Ejemplo n.º 32
0
    def test_pashto_in_pdf_language(self):
        """
            Tests if 'pashto' is the value of the 'language' attribute.
        """
        text = 'سلام. زه 3 ماشومان او 10 پیشوګانې لرم. دا په زړه پوری دی!'
        language_tag = '<pdf:language name="pashto"/>'

        html = self.HTML_CONTENT.format(ff_R=self.ff_R,
                                        ff_M=self.ff_M,
                                        ff_B=self.ff_B,
                                        ff_SB=self.ff_SB,
                                        ff_V=self.ff_V,
                                        text=text,
                                        language_tag=language_tag)

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=html, dest=pdf_file)

            self.assertEqual(pisa_doc.language, 'pashto',
                             '"pashto" not detected in <pdf:language>!')
Ejemplo n.º 33
0
    def test_urdu_in_pdf_language(self):
        """
            Tests if 'urdu' is the value of the 'language' attribute.
        """
        text = 'ہیلو. میرے 3 بچے اور 10 بلیاں ہیں۔ یہ تو زبردست ہے!'
        language_tag = '<pdf:language name="urdu"/>'

        html = self.HTML_CONTENT.format(ff_R=self.ff_R,
                                        ff_M=self.ff_M,
                                        ff_B=self.ff_B,
                                        ff_SB=self.ff_SB,
                                        ff_V=self.ff_V,
                                        text=text,
                                        language_tag=language_tag)

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=html, dest=pdf_file)

            self.assertEqual(pisa_doc.language, 'urdu',
                             '"urdu" not detected in <pdf:language>!')
Ejemplo n.º 34
0
    def test_arabic_in_pdf_language(self):
        """
            Tests if 'arabic' is the value of the 'language' attribute.
        """
        text = 'مرحبا. لدي 3 طفلاً و 10 قطة. هذا رائع!'
        language_tag = '<pdf:language name="arabic"/>'

        html = self.HTML_CONTENT.format(ff_R=self.ff_R,
                                        ff_M=self.ff_M,
                                        ff_B=self.ff_B,
                                        ff_SB=self.ff_SB,
                                        ff_V=self.ff_V,
                                        text=text,
                                        language_tag=language_tag)

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=html, dest=pdf_file)

            self.assertEqual(pisa_doc.language, 'arabic',
                             '"arabic" not detected in <pdf:language>!')
Ejemplo n.º 35
0
    def test_language_attribute_in_pisaDocument(self):
        """
            Tests if the attribute 'language' is located in the pisaDocument.
        """

        text = ''
        language_tag = '<pdf:language name=""/>'

        html = self.HTML_CONTENT.format(ff_R=self.ff_R,
                                        ff_M=self.ff_M,
                                        ff_B=self.ff_B,
                                        ff_SB=self.ff_SB,
                                        ff_V=self.ff_V,
                                        text=text,
                                        language_tag=language_tag)

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=html, dest=pdf_file)

            self.assertTrue(hasattr(pisa_doc, 'language'),
                            '<pdf:language> not found in the resulting PDF!')
Ejemplo n.º 36
0
    def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
        package = self.pool.get('stock.packages').browse(cr,
                                                         uid,
                                                         ids[0],
                                                         context=context)
        report_content = ''
        control_receipt = package.control_log_receipt
        if control_receipt:
            control_receipt = base64.decodestring(control_receipt)
            from xhtml2pdf.default import DEFAULT_CSS
            from xhtml2pdf.document import pisaDocument
            import sys
            import tempfile
            CreatePDF = pisaDocument
            path = tempfile.mktemp() + '.html'
            temp = file(path, 'wb')
            temp.write(control_receipt)
            temp.close()
            fsrc = open(path, 'rb')
            dest_file = tempfile.mktemp() + '.pdf'
            fdest = open(dest_file, 'wb')
            pdf = pisaDocument(fsrc,
                               fdest,
                               debug=0,
                               path=path,
                               errout=sys.stdout,
                               tempdir=None,
                               format='pdf',
                               link_callback=None,
                               default_css=None,
                               xhtml=False,
                               encoding=None,
                               xml_output=None)
            fdest.close()
            out_file = open(dest_file, 'rb')
            report_content = out_file.read()
            out_file.close()

        report_xml.report_type = "pdf"
        return (report_content, report_xml.report_type)
Ejemplo n.º 37
0
    def test_asian_font_in_pdf(self):
        """
            Tests if the asian fonts used in the CSS property "font-family"
            are correctly embeded in the pdf result.
        """

        # Read the embeded fonts from the finished pdf file
        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=self.HTML_CONTENT, dest=pdf_file)
            pdf_file.seek(0)
            pdf_content = PdfFileReader(pdf_file)
            pdf_fonts = read_fonts_from_pdf(pdf_content)

        # Read the fonts from the html content
        html_fonts = []
        for css_class in pisa_doc.css[0].values():
            for html_font_family in css_class.values():
                html_fonts.append(html_font_family)

        # Test, if all of the font-families from the html are also in the pdf file
        self.assertTrue(pdf_fonts.issuperset(html_fonts),
                        'Not all asian fonts detected in the PDF file!')
Ejemplo n.º 38
0
    def test_checking_content_in_context_as_frag_inCol(self):
        """
            this function is used to check if the text from pdf_file in page 2 is the one that should be
             according the grid system result form the html source
        """
        text = """{tag_img_two}
            Morado Lorem ipsum dolor sit amet, consectetur adipiscing elitv Lorem ipsum dolor sit
            amet, consectetur adipiscing elitv Lorem ipsum dolor sit amet, consectetur adipiscing elitv
            Lorem ipsum dolor sit amet, consectetur adipiscing elitv Lorem ipsum dolor sit amet,
            consectetur adipiscing elitv Lorem ipsum dolor sit amet, consectetur adipiscing elitv."""
        text = text.replace('\n            ','')

        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=self.html,
            dest=pdf_file)
            pdf_file.seek(0)
            pdf_content = PdfFileReader(pdf_file)

            page_two_text = pdf_content.getPage(2).extractText()
            page_two_text = page_two_text.replace('\n','')

        self.assertEquals(text, page_two_text)
Ejemplo n.º 39
0
    def test_check_updated_face_name(self):
        """
        this function help us to check is the font-family value on the pdf and
        the font-family from html element are same.
        """

        # Create the pisaDocument in memory from the HTML
        with io.BytesIO() as pdf_file:
            pisa_doc = pisaDocument(src=self.html, dest=pdf_file)

        # Parse HTML
        parser = html5lib.HTMLParser(
            tree=html5lib.treebuilders.getTreeBuilder("dom"))
        document = parser.parse(self.html)

        for spanElement in document.getElementsByTagName("span"):
            spanElement = CSSDOMElementInterface(spanElement)
            rules = pisa_doc.cssCascade.findCSSRulesFor(
                spanElement, "font-family")
            font_family_html = rules[0][1].get('font-family').lower()

            # Test if font-family of custom @font-face was added to the pisaDocument
            self.assertIsNotNone(pisa_doc.fontList.get(font_family_html))
    def test_check_updated_face_name(self):
        """
        this function help us to check is the font-family value on the pdf and
        the font-family from html element are same.
        """
        result = BytesIO()

        pdf = pisaDocument(BytesIO(self.html.encode('utf-8')), result)

        parser = html5lib.HTMLParser(
            tree=html5lib.treebuilders.getTreeBuilder("dom"))
        document = parser.parse(self.html)
        spanElement = document.getElementsByTagName("span")[0]
        spanElement = CSSDOMElementInterface(spanElement)
        attr_name = "font-family"
        rules = pdf.cssCascade.findCSSRulesFor(spanElement, attr_name)

        font_family = rules[0][1].get('font-family').strip('#')
        font_pdf = pdf.fontList.get('my')

        if isinstance(font_pdf, str):
            font_pdf = font_pdf.upper()

        self.assertEqual(font_family, font_pdf)
Ejemplo n.º 41
0
    def render(self):
        pdf = StringIO()

        def fetch_resources(uri, rel):
            """ Callback to allow pisa/reportlab to retrieve
            Images,Stylesheets, etc.

            `uri` is the href attribute from the html link element.
            `rel` gives a relative path, but it's not used here.

            See also: https://gist.github.com/2973355
            """
            urltool = getToolByName(self.context, 'portal_url')
            portal_url = urltool.getPortalObject().absolute_url()

            if uri.startswith(portal_url):

                response = subrequest(unquote(uri[len(portal_url) + 1:]))
                if response.status != 200:
                    return None
                try:
                    # Stupid pisa doesn't let me send charset.
                    ctype, encoding =\
                        response.getHeader('content-type').split('charset=')
                    ctype = ctype.split(';')[0]

                    # Pisa only likes ascii css
                    data = response.getBody()\
                        .decode(encoding).encode('ascii', 'ignore')
                except ValueError:
                    ctype = response.getHeader('content-type').split(';')[0]
                    data = response.getBody()

                data = data.encode('base64').replace('\n', '')
                data_uri = 'data:{0};base64,{1}'.format(ctype, data)

                # XXX: Pisa does not seem to be able to handle CSS-files as
                # data uris (it crashes), so lets return only images:
                if ctype.startswith('image'):
                    return data_uri

            return uri

        printview = self.context.restrictedTraverse("printview")

        html = printview().encode('utf-8', 'ignore')
        # Hide "Links"-section added by printview:
        html = html.replace('<div id="links">', '<div style="display: none;">')
        # Transform all links to absolute, to make them links in PDF:
        base_url = self.context.absolute_url()
        for url in re.findall(r'href="([^"]*)"', html):
            html = html.replace('href="%s"' % url,
                                'href="%s"' % urlparse.urljoin(base_url, url))
        html = StringIO(html)

        pisaDocument(html, pdf, raise_exception=True,
                     link_callback=fetch_resources, encoding='utf-8')
        assert pdf.len != 0, 'Pisa PDF generation returned empty PDF!'
        html.close()

        pdfcontent = pdf.getvalue()
        pdf.close()

        now = DateTime()
        nice_filename = '%s_%s' % (self.context.getId().capitalize(),
                                   now.strftime('%Y%m%d'))

        self.request.response.setHeader('Content-Disposition',
                                        'attachment; filename=%s.pdf' %
                                        nice_filename)
        self.request.response.setHeader('Content-Type', 'application/pdf')
        self.request.response.setHeader('Content-Length', len(pdfcontent))
        self.request.response.setHeader('Last-Modified',
                                        DateTime.rfc822(DateTime()))
        self.request.response.setHeader('Cache-Control', 'no-store')
        self.request.response.setHeader('Pragma', 'no-cache')
        self.request.response.write(pdfcontent)

        return pdfcontent
Ejemplo n.º 42
0
                height += max(l.ascent - l.descent, leading)
        elif autoLeading == 'min':
            for l in blPara.lines:
                height += l.ascent - l.descent
        else:
            raise ValueError('invalid autoLeading value %r' % autoLeading)
    else:
        if autoLeading == 'max':
            leading = max(leading, LEADING_FACTOR * style.fontSize)
        elif autoLeading == 'min':
            leading = LEADING_FACTOR * style.fontSize
        height = len(blPara.lines) * leading
    self.height = height
    return self.width, self.height


if __name__ == '__main__':
    import os
    from tornado.template import Loader

    font_file_path = os.path.join('/Users/yanglu/workspace/python/www-yestar-web/', 'configs', 'fonts', 'Microsoft_vista_yahei.ttf')
    pdfmetrics.registerFont(TTFont('yahei', font_file_path))

    fonts.addMapping('song', 0, 0, 'song')
    fonts.addMapping('song', 0, 1, 'song')
    DEFAULT_FONT['helvetica'] = 'yahei'
    xhtml2pdf.reportlab_paragraph.Paragraph.wrap = wrap
    loader = Loader('/Users/yanglu/workspace/python/www-yestar-web/app/views/ban_views/contract/copyright')
    t = loader.load('opus_register.html')
    pisaDocument(t.generate(font=font_file_path), open('test.pdf', 'w+'))
Ejemplo n.º 43
0
def execute():
#    from optparse import OptionParser
#
#    parser = OptionParser()
#    parser.add_option("-f", "--file", dest="filename",
#                      help="write report to FILE", metavar="FILE")
#    parser.add_option("-q", "--quiet",
#                      action="store_false", dest="verbose", default=True,
#                      help="don't print status messages to stdout")
#    (options, args) = parser.parse_args()

    try:
        opts, args = getopt.getopt(sys.argv[1:], "dhqstwcxb", [
            "quiet",
            "help",
            "start-viewer",
            "start",
            "debug=",
            "copyright",
            "version",
            "warn",
            #"booklet=",
            #"multivalent=",
            #"multivalent-path=",
            "tempdir=",
            "format=",
            "css=",
            "base=",
            "css-dump",
            "xml-dump",
            "xhtml",
            "xml",
            "html",
            "encoding=",
            "system",
            "profile",
            ])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    errors = 0
    startviewer = 0
    quiet = 0
    debug = 0
    #multivalent_path = ""
    #booklet = ""
    tempdir = None
    format = "pdf"
    css = None
    xhtml = None
    encoding = None
    xml_output = None
    base_dir = None

    log_level = logging.ERROR
    log_format = LOG_FORMAT

    for o, a in opts:

        if o in ("-h", "--help"):
            # Hilfe anzeigen
            usage()
            sys.exit()

        if o in ("-s", "--start-viewer", "--start"):
            # Anzeigeprogramm starten
            startviewer = 1

        if o in ("-q", "--quiet"):
            # Output unterdr�cken
            quiet = 1

        if o in ("-w", "--warn"):
            # Warnings
            log_level = min(log_level, logging.WARN) # If also -d ignore -w

        if o in ("-d", "--debug"):
            # Debug
            log_level = logging.DEBUG
            log_format = LOG_FORMAT_DEBUG
            # debug = 10
            if a:
                log_level = int(a)

#        if o in ("--multivalent", "--multivalent-path"):
#            # Multivalent.jar f�r Booklet
#            multivalent_path = a

#        if o in ("--booklet",):
#            # Booklet
#            booklet = a

        if o in ("--copyright", "--version"):
            print COPYRIGHT
            sys.exit(0)

        if o in ("--system",):
            print COPYRIGHT
            print
            print "SYSTEM INFORMATIONS"
            print "--------------------------------------------"
            print "OS:                ", sys.platform
            print "Python:            ", sys.version
            print "html5lib:          ", "?"
            import reportlab
            print "Reportlab:         ", reportlab.Version
            #try:
            #    import pyPdf
            #    print "pyPdf:             ", pyPdf.__version__
            #except:
            #    print "pyPdf:             ","-"
            sys.exit(0)

#        if o in ("--tempdir",):
#            # Tempdir
#            tempdir = a

        if o in ("-t", "--format"):
            # Format XXX ???
            format = a

        if o in ("-b","--base"):
            base_dir = a

        if o in ("--encoding",) and a:
            # Encoding
            encoding = a

        if o in ("-c", "--css"):
            # CSS
            # css = "@import url('%s');" % a
            css = file(a, "r").read()

        if o in ("--css-dump",):
            # CSS dump
            print DEFAULT_CSS
            return

        if o in ("--xml-dump",):
            xml_output = sys.stdout

        if o in ("-x", "--xml", "--xhtml"):
            xhtml = True
        elif o in ("--html",):
            xhtml = False

    if not quiet:
        try:
            logging.basicConfig(
                level=log_level,
                format=log_format)
        except:
            # XXX Logging doesn't work for Python 2.3
            logging.basicConfig()

    if len(args) not in (1, 2):
        usage()
        sys.exit(2)

    if len(args) == 2:
        a_src, a_dest = args
    else:
        a_src = args[0]
        a_dest = None

    if "*" in a_src:
        a_src = glob.glob(a_src)
        # print a_src
    else:
        a_src = [a_src]

    for src in a_src:

        # If not forced to parse in a special way have a look
        # at the filename suffix
        if xhtml is None:
            xhtml = src.lower().endswith(".xml")

        lc = None
        wpath = None

        if src == "-" or base_dir != None:
            # Output to console
            fsrc = sys.stdin
            wpath = os.getcwd()
            if base_dir:
                wpath = base_dir
        else:
            # fsrc = open(src, "r")
            if src.startswith("http:") or src.startswith("https:"):
                wpath = src
                fsrc = getFile(src).getFile()
                # fsrc = urllib2.urlopen(src)
                # lc = pisaLinkLoader(src, quiet=quiet).getFileName
                src = "".join(urlparse.urlsplit(src)[1:3]).replace("/", "-")
            else:
                fsrc = wpath = os.path.abspath(src)
                fsrc = open(fsrc, "rb")

        if a_dest is None:
            dest_part = src
            if dest_part.lower().endswith(".html") or dest_part.lower().endswith(".htm"):
                dest_part = ".".join(src.split(".")[:-1])
            dest = dest_part + "." + format.lower()
            for i in range(10):
                try:
                    open(dest, "wb").close()
                    break
                except:
                    pass
                dest = dest_part + "-%d.%s" % (i, format.lower())
        else:
            dest = a_dest

        fdestclose = 0

        if dest == "-" or base_dir:
            if sys.platform == "win32":
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
            fdest = sys.stdout
            startviewer = 0
        else:
            dest = os.path.abspath(dest)
            try:
                open(dest, "wb").close()
            except:
                print "File '%s' seems to be in use of another application." % dest
                sys.exit(2)
            fdest = open(dest, "wb")
            fdestclose = 1

        if not quiet:
            print "Converting %s to %s..." % (src, dest)

        pdf = pisaDocument(
            fsrc,
            fdest,
            debug = debug,
            path = wpath,
            errout = sys.stdout,
            #multivalent_path = multivalent_path,
            #booklet = booklet,
            tempdir = tempdir,
            format = format,
            link_callback = lc,
            default_css = css,
            xhtml = xhtml,
            encoding = encoding,
            xml_output = xml_output,
            )

        if xml_output:
            xml_output.getvalue()

        if fdestclose:
            fdest.close()

        if (not errors) and startviewer:
            if not quiet:
                print "Open viewer for file %s" % dest
            startViewer(dest)
def execute():

    try:
        opts, args = getopt.getopt(sys.argv[1:], "dhqstwcxb", [
            "quiet",
            "help",
            "start-viewer",
            "start",
            "debug=",
            "copyright",
            "version",
            "warn",
            "tempdir=",
            "format=",
            "css=",
            "base=",
            "css-dump",
            "xml-dump",
            "xhtml",
            "xml",
            "html",
            "encoding=",
            "system",
            "profile",
        ])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    errors = 0
    startviewer = 0
    quiet = 0
    debug = 0
    tempdir = None
    format = "pdf"
    css = None
    xhtml = None
    encoding = None
    xml_output = None
    base_dir = None

    log_level = logging.ERROR
    log_format = LOG_FORMAT

    for o, a in opts:

        if o in ("-h", "--help"):
            # Hilfe anzeigen
            usage()
            sys.exit()

        if o in ("-s", "--start-viewer", "--start"):
            # Anzeigeprogramm starten
            startviewer = 1

        if o in ("-q", "--quiet"):
            # Output unterdr�cken
            quiet = 1

        if o in ("-w", "--warn"):
            # Warnings
            log_level = min(log_level, logging.WARN)  # If also -d ignore -w

        if o in ("-d", "--debug"):
            # Debug
            log_level = logging.DEBUG
            log_format = LOG_FORMAT_DEBUG

            if a:
                log_level = int(a)

        if o in ("--copyright", "--version"):
            print (COPYRIGHT)
            sys.exit(0)

        if o in ("--system",):
            print (COPYRIGHT)
            print ()
            print ("SYSTEM INFORMATIONS")
            print ("--------------------------------------------")
            print ("OS:                ", sys.platform)
            print ("Python:            ", sys.version)
            print ("html5lib:          ", "?")
            import reportlab

            print ("Reportlab:         ", reportlab.Version)
            sys.exit(0)

        if o in ("-t", "--format"):
            # Format XXX ???
            format = a

        if o in ("-b", "--base"):
            base_dir = a

        if o in ("--encoding",) and a:
            # Encoding
            encoding = a

        if o in ("-c", "--css"):
            # CSS
            css = file(a, "r").read()

        if o in ("--css-dump",):
            # CSS dump
            print (DEFAULT_CSS)
            return

        if o in ("--xml-dump",):
            xml_output = sys.stdout

        if o in ("-x", "--xml", "--xhtml"):
            xhtml = True
        elif o in ("--html",):
            xhtml = False

    if not quiet:
        try:
            logging.basicConfig(
                level=log_level,
                format=log_format)
        except:
            # XXX Logging doesn't work for Python 2.3
            logging.basicConfig()

    if len(args) not in (1, 2):
        usage()
        sys.exit(2)

    if len(args) == 2:
        a_src, a_dest = args
    else:
        a_src = args[0]
        a_dest = None

    if "*" in a_src:
        a_src = glob.glob(a_src)
        # print a_src
    else:
        a_src = [a_src]

    for src in a_src:

        # If not forced to parse in a special way have a look
        # at the filename suffix
        if xhtml is None:
            xhtml = src.lower().endswith(".xml")

        lc = None

        if src == "-" or base_dir is not None:
            # Output to console
            fsrc = sys.stdin
            wpath = os.getcwd()
            if base_dir:
                wpath = base_dir
        else:
            if src.startswith("http:") or src.startswith("https:"):
                wpath = src
                fsrc = getFile(src).getFile()
                src = "".join(urlparse.urlsplit(src)[1:3]).replace("/", "-")
            else:
                fsrc = wpath = os.path.abspath(src)
                fsrc = open(fsrc, "rb")

        if a_dest is None:
            dest_part = src
            if dest_part.lower().endswith(".html") or dest_part.lower().endswith(".htm"):
                dest_part = ".".join(src.split(".")[:-1])
            dest = dest_part + "." + format.lower()
            for i in xrange(10):
                try:
                    open(dest, "wb").close()
                    break
                except:
                    pass
                dest = dest_part + "-%d.%s" % (i, format.lower())
        else:
            dest = a_dest

        fdestclose = 0

        if dest == "-" or base_dir:
            if sys.platform == "win32":
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

            fdest = sys.stdout
            startviewer = 0
        else:
            dest = os.path.abspath(dest)
            try:
                open(dest, "wb").close()
            except:
                print ("File '%s' seems to be in use of another application.") % dest
                sys.exit(2)
            fdest = open(dest, "wb")
            fdestclose = 1

        if not quiet:
            print ("Converting %s to %s...") % (src, dest)

        pdf = pisaDocument(
            fsrc,
            fdest,
            debug=debug,
            path=wpath,
            errout=sys.stdout,
            tempdir=tempdir,
            format=format,
            link_callback=lc,
            default_css=css,
            xhtml=xhtml,
            encoding=encoding,
            xml_output=xml_output,
        )

        if xml_output:
            xml_output.getvalue()

        if fdestclose:
            fdest.close()

        if (not errors) and startviewer:
            if not quiet:
                print ("Open viewer for file %s") % dest
            startViewer(dest)
Ejemplo n.º 45
0
def test_destination_is_none():
    context = pisaDocument(HTML_CONTENT.format(head="", extra_html=""))
    tools.assert_greater(len(context.dest.getvalue()), 0)
Ejemplo n.º 46
0
	def render_pdf(self, output_buffer):
		return not pisaDocument(cStringIO.StringIO(self.html), output_buffer).err
Ejemplo n.º 47
0
def pdf(uid):
    from xhtml2pdf.default import DEFAULT_FONT
    from xhtml2pdf.document import pisaDocument

    #########Set FONT################
    from reportlab.pdfbase import pdfmetrics
    from reportlab.pdfbase.ttfonts import TTFont
    pdfmetrics.registerFont(TTFont('zhfont', os.path.join(app.root_path, 'static/font/yahei-consolas.ttf')))
    DEFAULT_FONT["helvetica"]="zhfont"
    css = open(os.path.join(app.root_path, "static/css/pdf.css")).read()

    result = StringIO.StringIO()

    user = User.get(uid)
    if not g.user:
        ##匿名用户暂时只能看我的作为演示
        g.count = min(25, g.count)
        user = User.get(config.MY_USER_ID)
    else:
        if g.user.id == user.id:
            if g.count < 60:
                g.count = 60
            g.count = min(100, g.count)
        else:
            ##登录用户只能生成别人的25条
            g.count = min(25, g.count)

    # get status
    ids = Status.get_ids(user_id=uid, start=g.start, limit=g.count, cate=g.cate)
    status_list = Status.gets(ids)

    _html = u"""<html> <body>
        <div id="Top">
            <img src="%s"/> &nbsp; &nbsp;&nbsp; The Past of Me | 个人杂志计划&nbsp;&nbsp;&nbsp;%s&nbsp;&nbsp;&nbsp;CopyRight©%s
            <br/>
        </div>
        <br/> <br/>

        <div class="box">
    """ %(os.path.join(app.root_path, "static/img/logo.png"), 
        datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), user.name)

    for s in status_list:
        title = s.title
        create_time = s.create_time
        from_ = ''
        if s.category == config.CATE_DOUBAN_MINIBLOG:
            from_ = u'<a href="' + config.DOUBAN_MINIBLOG %(s.origin_user_id, s.origin_id) + u'class="node">From:豆瓣广播</a>'
        elif s.category == config.CATE_DOUBAN_NOTE:
            from_ = u'<a href="' + config.DOUBAN_NOTE %(s.origin_id,) + u'class="node">From:豆瓣日记</a>'
        elif s.category == config.CATE_SINA_STATUS:
            from_ = u'<a href="' + config.WEIBO_STATUS %(s.origin_id) + u'class="node">From:新浪微博</a>'
        elif s.category == config.CATE_TWITTER_STATUS:
            from_ = u'<a href="' + config.TWITTER_STATUS %(s.origin_id) + u'class="node">From:twitter</a>'
        text = s.text
        retweeted_text = ''
        img = ''
        if s.category == config.CATE_DOUBAN_MINIBLOG:
            ##miniblog不显示title
            title = ''
            links = s.get_data().get_links()
            if links and links.get("image"):
                img = links.get("image")
        elif s.category == config.CATE_SINA_STATUS:
            retweeted = s.get_data().get_retweeted_status()
            re_mid_pic = retweeted and retweeted.get_middle_pic() or ''
            middle_pic = s.get_data().get_middle_pic()

            if retweeted:
                retweeted_text = retweeted.get_user().get_nickname() + ": " + retweeted.get_content()
                    
            if re_mid_pic or middle_pic:
                img = re_mid_pic or middle_pic
        
        _html += """ <hr/> <div class="cell">"""
        if title:
            title = wrap_long_line(title)
            _html += """<div class="bigger">%s</div>""" %title
        if text:
            text = wrap_long_line(text)
            if s.category == config.CATE_DOUBAN_NOTE:
                text = filters.nl2br(text)
            _html += """<div class="content">%s</div>""" %text
        if retweeted_text:
            retweeted_text = wrap_long_line(retweeted_text)
            _html += """<div class='tip'><span class="fade">%s</span></div>""" %retweeted_text
        if img:
            _html += """<img src=%s></img>""" %img
        _html += """<div class="fade">%s &nbsp;&nbsp;&nbsp; %s</div>""" %(from_, create_time)
        _html += """ </div> <body> </html> """

    _pdf = pisaDocument(_html, result, default_css=css, link_callback=link_callback)

    if not _pdf.err:
        result.seek(0)
        pdf_filename = "thepast.me_pdf_%s%s.pdf" %(user.id, randbytes(6))
        save_pdf(result.getvalue(), pdf_filename)
        #resp = make_response(result.getvalue())
        #resp.headers["content-type"] = "application/pdf"
        resp = make_response()
        resp.headers['Cache-Control'] = 'no-cache'
        resp.headers['Content-Type'] = 'application/pdf'
        redir = '/down/pdf/' + pdf_filename
        resp.headers['X-Accel-Redirect'] = redir
        return resp
    else:
        return 'pdf error: %s' %_pdf.err