Rendering Static file in Wkhtmltopdf in Django
up vote
0
down vote
favorite
I have followed too many answers to this but I need some more explanation on this topic as I want to know the root cause for it.
I am trying to create pdf using wkhtmltopdf.
This is my setting files look like :
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And the URL to reference static file is :
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Or
<link rel="stylesheet" href="/static/css/template_pdf.css" type="text/css" />
or
<link rel="stylesheet" href="file:///static/css/template_pdf.css" type="text/css" />
or
I used this too:
https://gist.github.com/renyi/f02b4322590e9288ac679545df4748d3
and provided url as :
<link rel='stylesheet' type='text/css' href='{{ STATIC_URL }}static/css/template_pdf.css' />
But the issue I understood is, all of the above except last one works perfectly while rendering view :
def view_pdf(request):
"""View function for home page of site."""
context= {'title': 'Hello World!'}
# Render the HTML template index.html with the data in the context variable
return render(request, 'pdf/quotation.html', context=context)
But for creating pdf using wkhtmltopdf it specifically needs the url to be specified like :
<link rel="stylesheet" href="http:localhost:8000/static/css/template_pdf.css" type="text/css" />
I know I am missing something in the static file. But I want to know why it works with rendering template but not with Generating pdf using wkhtmltopdf.
I dont think it is good idea to put directly domain name inside the referencing url.
A detailed solution for this would be helpful as I am very new to django.
I tried follow this answer too but nothing worked : Django wkhtmltopdf don't reading static files
django wkhtmltopdf
|
show 7 more comments
up vote
0
down vote
favorite
I have followed too many answers to this but I need some more explanation on this topic as I want to know the root cause for it.
I am trying to create pdf using wkhtmltopdf.
This is my setting files look like :
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And the URL to reference static file is :
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Or
<link rel="stylesheet" href="/static/css/template_pdf.css" type="text/css" />
or
<link rel="stylesheet" href="file:///static/css/template_pdf.css" type="text/css" />
or
I used this too:
https://gist.github.com/renyi/f02b4322590e9288ac679545df4748d3
and provided url as :
<link rel='stylesheet' type='text/css' href='{{ STATIC_URL }}static/css/template_pdf.css' />
But the issue I understood is, all of the above except last one works perfectly while rendering view :
def view_pdf(request):
"""View function for home page of site."""
context= {'title': 'Hello World!'}
# Render the HTML template index.html with the data in the context variable
return render(request, 'pdf/quotation.html', context=context)
But for creating pdf using wkhtmltopdf it specifically needs the url to be specified like :
<link rel="stylesheet" href="http:localhost:8000/static/css/template_pdf.css" type="text/css" />
I know I am missing something in the static file. But I want to know why it works with rendering template but not with Generating pdf using wkhtmltopdf.
I dont think it is good idea to put directly domain name inside the referencing url.
A detailed solution for this would be helpful as I am very new to django.
I tried follow this answer too but nothing worked : Django wkhtmltopdf don't reading static files
django wkhtmltopdf
Unfortunately, that's how wkhtmltopdf works. There is nothing you can do about that, because wkhtmltopdf doesn't use your web server to render html and cannot resolve anything other than full path.
– Borut
yesterday
So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
yesterday
if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
yesterday
I don't know why that answer was accepted. Maybe it works like that with django-wkhtmltopdf package, but I can't tell, I'm not using it.
– Borut
yesterday
I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
yesterday
|
show 7 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have followed too many answers to this but I need some more explanation on this topic as I want to know the root cause for it.
I am trying to create pdf using wkhtmltopdf.
This is my setting files look like :
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And the URL to reference static file is :
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Or
<link rel="stylesheet" href="/static/css/template_pdf.css" type="text/css" />
or
<link rel="stylesheet" href="file:///static/css/template_pdf.css" type="text/css" />
or
I used this too:
https://gist.github.com/renyi/f02b4322590e9288ac679545df4748d3
and provided url as :
<link rel='stylesheet' type='text/css' href='{{ STATIC_URL }}static/css/template_pdf.css' />
But the issue I understood is, all of the above except last one works perfectly while rendering view :
def view_pdf(request):
"""View function for home page of site."""
context= {'title': 'Hello World!'}
# Render the HTML template index.html with the data in the context variable
return render(request, 'pdf/quotation.html', context=context)
But for creating pdf using wkhtmltopdf it specifically needs the url to be specified like :
<link rel="stylesheet" href="http:localhost:8000/static/css/template_pdf.css" type="text/css" />
I know I am missing something in the static file. But I want to know why it works with rendering template but not with Generating pdf using wkhtmltopdf.
I dont think it is good idea to put directly domain name inside the referencing url.
A detailed solution for this would be helpful as I am very new to django.
I tried follow this answer too but nothing worked : Django wkhtmltopdf don't reading static files
django wkhtmltopdf
I have followed too many answers to this but I need some more explanation on this topic as I want to know the root cause for it.
I am trying to create pdf using wkhtmltopdf.
This is my setting files look like :
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And the URL to reference static file is :
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Or
<link rel="stylesheet" href="/static/css/template_pdf.css" type="text/css" />
or
<link rel="stylesheet" href="file:///static/css/template_pdf.css" type="text/css" />
or
I used this too:
https://gist.github.com/renyi/f02b4322590e9288ac679545df4748d3
and provided url as :
<link rel='stylesheet' type='text/css' href='{{ STATIC_URL }}static/css/template_pdf.css' />
But the issue I understood is, all of the above except last one works perfectly while rendering view :
def view_pdf(request):
"""View function for home page of site."""
context= {'title': 'Hello World!'}
# Render the HTML template index.html with the data in the context variable
return render(request, 'pdf/quotation.html', context=context)
But for creating pdf using wkhtmltopdf it specifically needs the url to be specified like :
<link rel="stylesheet" href="http:localhost:8000/static/css/template_pdf.css" type="text/css" />
I know I am missing something in the static file. But I want to know why it works with rendering template but not with Generating pdf using wkhtmltopdf.
I dont think it is good idea to put directly domain name inside the referencing url.
A detailed solution for this would be helpful as I am very new to django.
I tried follow this answer too but nothing worked : Django wkhtmltopdf don't reading static files
django wkhtmltopdf
django wkhtmltopdf
edited yesterday
asked yesterday
CodeGeek
408
408
Unfortunately, that's how wkhtmltopdf works. There is nothing you can do about that, because wkhtmltopdf doesn't use your web server to render html and cannot resolve anything other than full path.
– Borut
yesterday
So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
yesterday
if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
yesterday
I don't know why that answer was accepted. Maybe it works like that with django-wkhtmltopdf package, but I can't tell, I'm not using it.
– Borut
yesterday
I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
yesterday
|
show 7 more comments
Unfortunately, that's how wkhtmltopdf works. There is nothing you can do about that, because wkhtmltopdf doesn't use your web server to render html and cannot resolve anything other than full path.
– Borut
yesterday
So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
yesterday
if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
yesterday
I don't know why that answer was accepted. Maybe it works like that with django-wkhtmltopdf package, but I can't tell, I'm not using it.
– Borut
yesterday
I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
yesterday
Unfortunately, that's how wkhtmltopdf works. There is nothing you can do about that, because wkhtmltopdf doesn't use your web server to render html and cannot resolve anything other than full path.
– Borut
yesterday
Unfortunately, that's how wkhtmltopdf works. There is nothing you can do about that, because wkhtmltopdf doesn't use your web server to render html and cannot resolve anything other than full path.
– Borut
yesterday
So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
yesterday
So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
yesterday
if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
yesterday
if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
yesterday
I don't know why that answer was accepted. Maybe it works like that with django-wkhtmltopdf package, but I can't tell, I'm not using it.
– Borut
yesterday
I don't know why that answer was accepted. Maybe it works like that with django-wkhtmltopdf package, but I can't tell, I'm not using it.
– Borut
yesterday
I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
yesterday
I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
yesterday
|
show 7 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
in your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'
to render static files in your template django provide static tag. You can use this as
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Also make sure you have this included in your urls.py
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Don't forget to run collectstatic
command in the end
It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
yesterday
update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
yesterday
it is already there
– CodeGeek
yesterday
see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
yesterday
I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
yesterday
|
show 11 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
in your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'
to render static files in your template django provide static tag. You can use this as
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Also make sure you have this included in your urls.py
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Don't forget to run collectstatic
command in the end
It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
yesterday
update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
yesterday
it is already there
– CodeGeek
yesterday
see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
yesterday
I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
yesterday
|
show 11 more comments
up vote
0
down vote
in your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'
to render static files in your template django provide static tag. You can use this as
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Also make sure you have this included in your urls.py
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Don't forget to run collectstatic
command in the end
It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
yesterday
update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
yesterday
it is already there
– CodeGeek
yesterday
see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
yesterday
I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
yesterday
|
show 11 more comments
up vote
0
down vote
up vote
0
down vote
in your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'
to render static files in your template django provide static tag. You can use this as
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Also make sure you have this included in your urls.py
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Don't forget to run collectstatic
command in the end
in your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
WKHTMLTOPDF_CMD = '/usr/local/bin/wkhtmltopdf'
to render static files in your template django provide static tag. You can use this as
<link rel="stylesheet" href="{% static '/css/template_pdf.css' %}" type="text/css" />
Also make sure you have this included in your urls.py
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Don't forget to run collectstatic
command in the end
edited yesterday
answered yesterday
Sumeet Kumar
6511511
6511511
It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
yesterday
update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
yesterday
it is already there
– CodeGeek
yesterday
see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
yesterday
I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
yesterday
|
show 11 more comments
It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
yesterday
update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
yesterday
it is already there
– CodeGeek
yesterday
see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
yesterday
I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
yesterday
It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
yesterday
It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
yesterday
update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
yesterday
update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
yesterday
it is already there
– CodeGeek
yesterday
it is already there
– CodeGeek
yesterday
see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
yesterday
see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
yesterday
I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
yesterday
I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
yesterday
|
show 11 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238084%2frendering-static-file-in-wkhtmltopdf-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Unfortunately, that's how wkhtmltopdf works. There is nothing you can do about that, because wkhtmltopdf doesn't use your web server to render html and cannot resolve anything other than full path.
– Borut
yesterday
So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
yesterday
if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
yesterday
I don't know why that answer was accepted. Maybe it works like that with django-wkhtmltopdf package, but I can't tell, I'm not using it.
– Borut
yesterday
I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
yesterday