Cloud Stack Ninja

My PDF.html is not linking to any CSS file, I have tried the following:

Template PDF.HTML

{% load static %}

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<link href='{% static "css/bootstrap.min.css" %}' rel="stylesheet">
<!-- Material Design Bootstrap -->
<link href='{% static "css/mdb.min.css" %}' rel="stylesheet">
<!-- Your custom styles (optional) -->
<link href='{% static "css/style.min.css" %}' rel="stylesheet">

I am using the same CSS files for other templates as base.html which is working fine and linking to them but for this particular HTML it is not linking, so I am trying to find the reason for this issue

Here is the base.html

{% load static %}

<!DOCTYPE html>
<html lang="en">

<head>
-----------------------------------------------------

<link href='{% static "css/bootstrap.min.css" %}' rel="stylesheet">
<!-- Material Design Bootstrap -->
<link href='{% static "css/mdb.min.css" %}' rel="stylesheet">
<!-- Your custom styles (optional) -->
<link href='{% static "css/style.min.css" %}' rel="stylesheet">

I am using weazyprint to get the pdf so here is the views I have tried to use it but it is returned FileNotFoundError at /admin/order/50/pdf/ [Errno 2] No such file or directory: 'C:\\Users\\User\\Desktop\\static_rootcss/pdf.css'

@staff_member_required
def admin_order_pdf(request, order_id):
    order = get_object_or_404(Order,ordered=True, id=order_id)
    html = render_to_string('pdf.html', {'order': order})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = f'filename="order_{order.id}.pdf"'    
    weasyprint.HTML(string=html).write_pdf(response, 
                    stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')])
    return response

I am using the followig config in the settings.py

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')


Read more here: https://stackoverflow.com/questions/64409042/how-to-link-css-file-to-pdf-html-file-for-django-fixing-a-filenotfounderror

Content Attribution

This content was originally published by A_K at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: