[英]Printing a PDF file in Django
I want to create something in my Django project that physically prints a PDF file in the FileField of a model object onto paper. 我想在我的Django项目中创建一些内容,以便将模型对象的FileField中的PDF文件实际打印到纸上。 Like so:
像这样:
class PDF(models.Model):
pdf = models.FileField(upload_to='pdffiles/', blank=True, null=True}
The main thing I want to do is make a link that creates a popup using Javascript that contains an input field where the user puts the name of the PDF from the object's FileField and a button that says "Print" that triggers the physical printing function (including opening the Print Dialog Box). 我要做的主要事情是建立一个链接,该链接使用Javascript创建一个弹出窗口,其中包含一个输入字段,用户在其中输入对象的FileField中PDF的名称,以及一个显示“ Print”的按钮,该按钮触发物理打印功能(包括打开“打印”对话框)。 Am I supposed to use forms or views in order to make this function, and if I'm supposed to use Javascript to activate the printing function, how would I do it?
我应该使用表格或视图来实现此功能吗?如果我要使用Javascript来激活打印功能,我该怎么做? Thanks.
谢谢。
EDIT: I'm thinking of using print.js. 编辑:我正在考虑使用print.js。 Could someone tell me how to implement print.js in my Django project?
有人可以告诉我如何在Django项目中实现print.js吗? What files from the Git repository do I need to insert and how do I link them to my templates?
我需要从Git存储库中插入哪些文件,如何将它们链接到模板?
If I following this Question , I think you can using this solution; 如果我遵循此Question ,我认为您可以使用此解决方案。
In your views.py
在您的
views.py
from django.conf import settings
from django.shortcuts import get_object_or_404
from yourapp.models import PDF
def pdf_viewer(request, pk):
obj = get_object_or_404(PDF, pk=pk)
pdf_full_path = settings.BASE_DIR + obj.pdf.url
with open(pdf_full_path, 'r') as pdf:
response = HttpResponse(pdf.read(), content_type='application/pdf')
response['Content-Disposition'] = 'filename=%s' % obj.pdf.name
return response
pdf.closed
and then urls.py
; 然后
urls.py
;
from django.conf.urls import url
from yourapp.views import pdf_viewer
urlpatterns = [
url(r'^pdf-viewer/(?P<pk>\d+)/$', pdf_viewer, name='pdf_viewer_page'),
]
How about inside the template? 在模板内部如何?
<button class="show-pdf">Show PDF</button>
<div class="pdf-wrapper">
<iframe id="pdf-iframe" frameborder="0" allowfullscreen></iframe>
</div>
<script>
// you can using jQuery to load the pdf file as iframe.
$('.show-pdf').click(function () {
var src = '{% url "pdf_viewer_page" pk=obj.id %}';
var iframe = $("#pdf-iframe");
iframe.attr({'width': 560, 'height': 300});
// iframe.attr('src', src); // to load the pdf file only.
// to load and auto print the pdf file.
iframe.attr('src', src).load(function(){
document.getElementById('pdf-iframe').contentWindow.print();
});
return false;
});
</script>
But makesure in this template if you try with
{{ obj.pdf.url }}
should return string of eg:'/media/to/file.pdf'
但是,如果尝试使用
{{ obj.pdf.url }}
,则此模板中的{{ obj.pdf.url }}
应该返回字符串,例如:'/media/to/file.pdf'
Or more easier (full page) ; 或更容易(整页) ;
<a href='{% url "pdf_viewer_page" pk=obj.id %}' target="_blank">Show PDF on new tab</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.