S
S
Sergey Nizhny Novgorod2016-03-23 04:14:13
Django
Sergey Nizhny Novgorod, 2016-03-23 04:14:13

How to give an html page and then convert it to PDF?

Hello.
What I do:
1) Parse the pages I need.
2) With this data, I dynamically render an html page.
3) I'm trying to convert an html page to pdf and give it to users.
I'm trying to do just that, the kibo parser receives the html code (not always amenable to normal parsing) and I bring it to an adequate form through css and html.
I'm trying to do something like:
The error is that the PDF convector does not want to accept the rendering of the html page.

def parse(request):
    done = csrf(request)
    if request.POST: # начало парсера
        USERNAME = request.POST.get('logins', '')
        PASSWORD = request.POST.get('password', '')
        dialogue_url = request.POST.get('links', '')
        total_pages = int(request.POST.get('numbers', ''))
        news = []
        news.extend(parse_one(USERNAME, PASSWORD, dialogue_url, total_pages))  #вызываю функцию парсера 
        contex = {
                    "news" : news,
                }
        done.update(contex) #конец парсера

        pageclan = render(request, 'marketing/parser.html', done) # как-то так пытаюсь рендерить страницу и сохранить её в переменную
        

        # create an API client instance #начало pdf форминга
        client = pdfcrowd.Client("username", "key")

        # convert a web page and store the generated PDF to a variable
        pdf = client.convertURI(pageclan)

         # set HTTP response headers
        response = HttpResponse(content_type="application/pdf")
        response["Cache-Control"] = "max-age=0"
        response["Accept-Ranges"] = "none"
        response["Content-Disposition"] = "attachment; filename=jivo_log.pdf"

        # send the generated PDF
        response.write(pdf)
        return response

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-03-23
@Terras

pageclan contains an HttpResponse object, and convertURI takes a page reference. Need something like

tmpl = get_template('marketing/parser.html')
html = tmpl.render(done)
pdf = client.convertHtml(html)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question