2016-11-15 123 views
1

我有一個任務,使用動態數據呈現HTML頁面並將其轉換爲PDF,但是,實現它時遇到問題。如何呈現HTML Django頁面爲PDF?

def makepdf(request): 
    if request.POST: 
     chatvs = Competirors.objects.get(id = int(request.POST.get('competitor', ''))) 
     jivo_staff = Manager.objects.get(id = int(request.POST.get('manager', ''))) 

     business = Field.objects.get(id = int(request.POST.get('filed', ''))) 

     business = business.link.all().order_by('?')[0:3]   

     context = { 
      "chatvs" : chatvs, 
      "jivo_staff" : jivo_staff, 
      "business" : business, 
     } 

     tmpl = get_template('marketing/jivopdf.html',) 
     html = tmpl.render(context) 

     # and i have a problem there with pdfkit 

我正在使用Pdfcrowd API,但它削減了所有的CSS樣式,看起來不太好。至於vvwkhtmltopdf/pdfkit - 我不知道如何使它成爲一個呈現的HTML頁面,因爲它只訪問url/file/string。

有沒有辦法呈現準備好的HTML頁面?

回答

0

很難說,不知道你得到的錯誤。

但是,我可以從您的代碼中看到,如果這是您的真實代碼。看起來像Competirors.objects.get中的拼寫錯誤,應該可能被命名爲競爭對手。

如果您從表單中獲取POST數據,則不應直接使用它。 永遠不要相信用戶輸入...