javascript - Printing a PDF file in Django -


i want create in django project physically prints pdf file in filefield of model object onto paper. so:

class pdf(models.model):     pdf = models.filefield(upload_to='pdffiles/', blank=true, null=true} 

the main thing want make link creates popup using javascript contains input field user puts name of pdf object's filefield , button says "print" triggers physical printing function (including opening print dialog box). supposed use forms or views in order make function, , if i'm supposed use javascript activate printing function, how it? thanks.

edit: i'm thinking of using print.js. tell me how implement print.js in django project? files git repository need insert , how link them templates?

if following question, think can using solution;

in views.py

from django.conf import settings django.shortcuts import get_object_or_404 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     open(pdf_full_path, 'r') pdf:         response = httpresponse(pdf.read(), content_type='application/pdf')         response['content-disposition'] = 'filename=%s' % obj.pdf.name         return response     pdf.closed 

and urls.py;

from django.conf.urls import url yourapp.views import pdf_viewer  urlpatterns = [     url(r'^pdf-viewer/(?p<pk>\d+)/$', pdf_viewer, name='pdf_viewer_page'), ] 

how inside template?

<button class="show-pdf">show pdf</button> <div class="pdf-wrapper">   <iframe id="pdf-iframe" frameborder="0" allowfullscreen></iframe> </div>  <script> // can using jquery load pdf file 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); // load pdf file only.      // load , auto print pdf file.     iframe.attr('src', src).load(function(){       document.getelementbyid('pdf-iframe').contentwindow.print();     });     return false; }); </script> 

but makesure in template if try {{ obj.pdf.url }} should return string of eg: '/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> 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -