CSS not showing in Pisa pdf in Django

I am creating a pdf file from HTML using Pisa:

def fetch_resources(uri, rel): path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, "")) return path def write_pdf(template_src, context_dict, filename): template = get_template(template_src) context = Context(context_dict) html = template.render(context) result = open(filename, 'wb') pdf = pisa.pisaDocument(StringIO.StringIO( html.encode("UTF-8")), result, link_callback=fetch_resources) result.close() 

My HTML has a link to external CSS and displays correctly, but CSS is not used by Pisa (e.g. font size, table cell width, text alignment ...).

 <!DOCTYPE html> <html lang="fr"> <head> <link rel="stylesheet" href="/site_media/style/style.css" /> </head> <body> .... 

Did I miss something?

thanks

+6
source share
1 answer

You can try this "Pisa-and-Reportlab-Trap" I had to add this

 def fetch_resources(uri, rel): 

In addition, I still carry all my CSS in the template. Also make sure you use xhtml2pdf and not the old ho.pisa.

+6
source

Source: https://habr.com/ru/post/906756/


All Articles