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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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






share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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






share|improve this answer























  • 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

















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






share|improve this answer























  • 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















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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues