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
    Nov 10 at 11:27










  • So If I switch between debug and production, I have to change the address everytime ?
    – CodeGeek
    Nov 10 at 11:28












  • if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
    – CodeGeek
    Nov 10 at 11:32










  • 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
    Nov 10 at 11:37










  • I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
    – CodeGeek
    Nov 10 at 11:37















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
    Nov 10 at 11:27










  • So If I switch between debug and production, I have to change the address everytime ?
    – CodeGeek
    Nov 10 at 11:28












  • if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
    – CodeGeek
    Nov 10 at 11:32










  • 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
    Nov 10 at 11:37










  • I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
    – CodeGeek
    Nov 10 at 11:37













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 Nov 10 at 11:06

























asked Nov 10 at 10:35









CodeGeek

518




518












  • 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
    Nov 10 at 11:27










  • So If I switch between debug and production, I have to change the address everytime ?
    – CodeGeek
    Nov 10 at 11:28












  • if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
    – CodeGeek
    Nov 10 at 11:32










  • 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
    Nov 10 at 11:37










  • I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
    – CodeGeek
    Nov 10 at 11:37


















  • 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
    Nov 10 at 11:27










  • So If I switch between debug and production, I have to change the address everytime ?
    – CodeGeek
    Nov 10 at 11:28












  • if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
    – CodeGeek
    Nov 10 at 11:32










  • 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
    Nov 10 at 11:37










  • I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
    – CodeGeek
    Nov 10 at 11:37
















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
Nov 10 at 11:27




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
Nov 10 at 11:27












So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
Nov 10 at 11:28






So If I switch between debug and production, I have to change the address everytime ?
– CodeGeek
Nov 10 at 11:28














if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
Nov 10 at 11:32




if it is so than how this answer was accepted : stackoverflow.com/questions/19445486/…
– CodeGeek
Nov 10 at 11:32












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
Nov 10 at 11:37




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
Nov 10 at 11:37












I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
Nov 10 at 11:37




I am using django-wkhtmltopdf only. Still I tried that thing but it doesnt work.
– CodeGeek
Nov 10 at 11:37












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
    Nov 10 at 10:44












  • update your question with static settings.. so that it is easy to identify the issue
    – Sumeet Kumar
    Nov 10 at 10:55










  • it is already there
    – CodeGeek
    Nov 10 at 11:06










  • see my updated answer, you need to include these lines in urls.py
    – Sumeet Kumar
    Nov 10 at 11:18










  • I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
    – CodeGeek
    Nov 10 at 11:21













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















Required, but never shown

























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
    Nov 10 at 10:44












  • update your question with static settings.. so that it is easy to identify the issue
    – Sumeet Kumar
    Nov 10 at 10:55










  • it is already there
    – CodeGeek
    Nov 10 at 11:06










  • see my updated answer, you need to include these lines in urls.py
    – Sumeet Kumar
    Nov 10 at 11:18










  • I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
    – CodeGeek
    Nov 10 at 11:21

















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
    Nov 10 at 10:44












  • update your question with static settings.. so that it is easy to identify the issue
    – Sumeet Kumar
    Nov 10 at 10:55










  • it is already there
    – CodeGeek
    Nov 10 at 11:06










  • see my updated answer, you need to include these lines in urls.py
    – Sumeet Kumar
    Nov 10 at 11:18










  • I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
    – CodeGeek
    Nov 10 at 11:21















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 Nov 10 at 13:24

























answered Nov 10 at 10:43









Sumeet Kumar

6511511




6511511












  • It works while rendering view not while generating PDF using wkhtmltopdf
    – CodeGeek
    Nov 10 at 10:44












  • update your question with static settings.. so that it is easy to identify the issue
    – Sumeet Kumar
    Nov 10 at 10:55










  • it is already there
    – CodeGeek
    Nov 10 at 11:06










  • see my updated answer, you need to include these lines in urls.py
    – Sumeet Kumar
    Nov 10 at 11:18










  • I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
    – CodeGeek
    Nov 10 at 11:21




















  • It works while rendering view not while generating PDF using wkhtmltopdf
    – CodeGeek
    Nov 10 at 10:44












  • update your question with static settings.. so that it is easy to identify the issue
    – Sumeet Kumar
    Nov 10 at 10:55










  • it is already there
    – CodeGeek
    Nov 10 at 11:06










  • see my updated answer, you need to include these lines in urls.py
    – Sumeet Kumar
    Nov 10 at 11:18










  • I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
    – CodeGeek
    Nov 10 at 11:21


















It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
Nov 10 at 10:44






It works while rendering view not while generating PDF using wkhtmltopdf
– CodeGeek
Nov 10 at 10:44














update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
Nov 10 at 10:55




update your question with static settings.. so that it is easy to identify the issue
– Sumeet Kumar
Nov 10 at 10:55












it is already there
– CodeGeek
Nov 10 at 11:06




it is already there
– CodeGeek
Nov 10 at 11:06












see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
Nov 10 at 11:18




see my updated answer, you need to include these lines in urls.py
– Sumeet Kumar
Nov 10 at 11:18












I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
Nov 10 at 11:21






I kept this too. But no change in generating pdf. Rendering template view is correct as it was.
– CodeGeek
Nov 10 at 11:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff