Got it working now. Problem was that it was that pdfkit was outputting
everything... even verbose stuff, would have thought that would have been
off. Thanks for your help.
On Monday, January 26, 2015 at 8:20:55 AM UTC+8, Adam Morris wrote:
>
> Okay, I've identified pdfkit but with the following code I get a blank pdf
> downloaded, I must be doing something wrong...:
>
> from pyramid.renderers import render
> from pyramid.response import Response
> import pdfkit
> import StringIO
>
>
> result = render('frontend:templates/student_pyp_report.pt',
> dict(
> title=title,
> report=report,
> student=student,
> pdf=True
> ),
> request=request)
>
>
> pdffileio = StringIO.StringIO()
> pdffileio.write(result)
>
>
> pdf_as_string = pdfkit.from_file(pdffileio, False) # False
> means return it as a string
>
>
> response = Response(
> pdf_as_string,
> content_disposition="attachment; filename={}.pdf".format(
> "test"),
> content_type = "application/pdf",
> charset="utf-8"
> )
>
>
> #pdffileio.close()
>
>
> return response
>
>
> and the pdf_as_string is just this:
>
> Loading page (1/2)\n[>
> ] 0%\r[======> ]
> 10%\r[============================================================]
> 100%\rPrinting pages (2/2)
> \n%PDF-1.4\n1 0 obj\n<<\n/Title (\xfe\xff)\n/Creator (\xfe\xff)\n/Producer
> (\xfe\xff\x00Q\x00t\x00 \x004\x00.\x008\x00.\x006)\n/CreationDate
> (D:20150126001805)\n>>\nendobj\n2 0 obj\n<<\n/Type /Catalog\n/Pages 3 0
> R\n>>\nendobj\n4 0 obj\n<<\n/Type /ExtGState\n/SA true\n/SM 0.02\n/ca
> 1.0\n/CA 1.0\n/AIS false\n/SMask /None>>\nendobj\n5 0 obj\n[/Pattern
> /DeviceRGB]\nendobj\n6 0 obj\n<<\n/Type /Page\n/Parent 3 0 R\n/Contents 7 0
> R\n/Resources 9 0 R\n/Annots 10 0 R\n/MediaBox [0 0 595 842]\n>>\nendobj\n9
> 0 obj\n<<\n/ColorSpace <<\n/PCSp 5 0 R\n/CSp /DeviceRGB\n/CSpg
> /DeviceGray\n>>\n/ExtGState <<\n/GSa 4 0 R\n>>\n/Pattern <<\n>>\n/Font
> <<\n>>\n/XObject <<\n>>\n>>\nendobj\n10 0 obj\n[ ]\nendobj\n7 0
> obj\n<<\n/Length 8 0 R\n/Filter
> /FlateDecode\n>>\nstream\nx\x9c\xadP=\x0b\x021\x0c\xdd\xf3+2\x0b\xf6\xd2\x16\xda\xde|\x83\xe0
>
> \x1c\x1d\x1c\xc4A\xce/\xc4;\xac7\xf8\xf7M?\x94\xc3I\xd0\x04\xfa\xf2B>^S-\xfc\x0eO#V\x8d\xbfaW\xb0\xf1@\x82\x0ce\xc3\xe8\xf3iB9\xa1U\x0e\x9d\xd4\xc2\xd8\x9a\r\xbb\x1e\x02\x06h\xa1\xe57b\x00\xa9Rs\x01.x\xad\xc9C\xc7n\x80*\x0b\x80\x9c\xf1\xcd\x8a\xa3\x07*\\2\xbb\xe0f\xcb\xb0/3cA\x0f\xae6Z\xd4i#\xe1uJ\xa5&k\x84$\xe58O\x9f4\x16\x9fa=\xc3\x81\x85\xd5\xc2\x90\xb5\xd2\x92Nk?\xe8/B\xc3\xbbU&\x9f\xb6~\xb17e\xb5f\xf1F\xe2\xfd\x00\xc7\xf8\xf7r\xd1\xbf]\x13[x\x02\xe7@lMendstream\nendobj\n8
>
> 0 obj\n194\nendobj\n3 0 obj\n<<\n/Type /Pages\n/Kids \n[\n6 0 R\n]\n/Count
> 1\n/ProcSet [/PDF /Text /ImageB /ImageC]\n>>\nendobj\nxref\n0
> 11\n0000000000 65535 f \n0000000009 00000 n \n0000000120 00000 n
> \n0000000881 00000 n \n0000000169 00000 n \n0000000264 00000 n \n0000000301
> 00000 n \n0000000595 00000 n \n0000000862 00000 n \n0000000420 00000 n
> \n0000000575 00000 n \ntrailer\n<<\n/Size 11\n/Info 1 0 R\n/Root 2 0
> R\n>>\nstartxref\n979\n%%EOF\n[>
> ] \rDone
> \n
>
>
> On Monday, January 26, 2015 at 1:17:19 AM UTC+8, Wichert Akkerman wrote:
>>
>>
>> On 25 Jan 2015, at 14:40, Adam Morris <[email protected]> wrote:
>>
>> Okay, I get that bit now, and coded it up, but when I go to render it, I
>> use the pyramid.renderers.render object but obviously it ends up
>> downloading a corrupted file because it's not even in PDF format.
>>
>>
>> You are still rendering a .pt file, so I’m guessing you are generating
>> HTML, not PDF. If you want to generate PDF you will need to use a PDF
>> library.
>>
>> So does reportlab have something to take that html/css and paint it to a
>> Canvas or something?
>>
>>
>> I use z3c.rml, which uses RML as input to generate PDF. If you want to
>> use HTML & CSS you’ll need to find a library that can convert HTML to PDF.
>>
>> Wichert.
>>
>>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.