2

I want to create something in my Django project that physically prints a PDF file in the FileField of a model object onto paper. 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). 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? Thanks.

EDIT: I'm thinking of using print.js. Could someone tell me how to implement print.js in my Django project? What files from the Git repository do I need to insert and how do I link them to my templates?

Jerry Bi
  • 321
  • 6
  • 24

1 Answers1

3

If I following this Question, I think you can using this solution;

In your 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;

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'

Or more easier (full page);

<a href='{% url "pdf_viewer_page" pk=obj.id %}' target="_blank">Show PDF on new tab</a>
binpy
  • 3,994
  • 3
  • 17
  • 54
  • I need a link that creates a modal that has an input field that allows the user to type in the name of the file or choose from a dropdown box, and a button that prints out the PDF file who's name matches the one the user inputted. Is there a way to do this without needing to view the PDF? That would be preferred, but not necessary. Also, I see the code in your that initiates the printing process, but what would a person need to do or click on to activate it? – Jerry Bi Aug 20 '17 at 17:38
  • Does this work with files hosted on for example S3? – Redgren Grumbholdt May 08 '19 at 11:12