How to open the generated PDF file in a browser?

I wrote a Pdf merge that combines the source file with a watermark.

Now I want to open the document-output.pdf file in a browser using the Django view. I have already checked out articles related to Django, but since my approach is relatively different, I do not directly create a PDF object using the response object as my "file". Therefore, I have lost.

So how can I do this in a Django view?

from pyPdf import PdfFileWriter, PdfFileReader from reportlab.pdfgen.canvas import Canvas from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont output = PdfFileWriter() input = PdfFileReader(file('file.pdf', 'rb')) # get number of pages num_pages = input.getNumPages() # register new chinese font pdfmetrics.registerFont(TTFont('chinese_font','/usr/share/fonts/truetype/mac/LiHeiPro.ttf')) # generate watermark on the fly pdf = Canvas("watermark.pdf") pdf.setFont("chinese_font", 12) pdf.setStrokeColorRGB(0.5, 1, 0) pdf.drawString(10, 830, "你好") pdf.save() # put on watermark watermark = PdfFileReader(file('watermark.pdf', 'rb')) page1 = input.getPage(0) page1.mergePage(watermark.getPage(0)) # add processed pdf page output.addPage(page1) # then, add rest of pages for num in range(1, num_pages): output.addPage(input.getPage(num)) outputStream = file("document-output.pdf", "wb") output.write(outputStream) outputStream.close() 
+6
django pypdf reportlab
source share
4 answers

I know its older record, but we can use the embed tag for html to implement this kind of function. For example, for example:

 <embed height="100%" width="100%" name="plugin" src="filename.pdf" type="application/pdf"> 

So, in your case, you can simply send the answer using a render in response as:

 return render_to_response("abc.html",{"filename":filename}) 

and in abc.html you can put this file name (using the path) in the embed tag, as described above.

Hope this helps.

+9
source share

In addition to sending your PDF back to the browser, you can also save several cycles by storing the watermark in a string buffer.

 from pyPdf import PdfFileWriter, PdfFileReader from reportlab.pdfgen.canvas import Canvas from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from django.http import HttpResponse try: from cStringIO import StringIO except ImportError: from StringIO import StringIO def some_view(request): output = PdfFileWriter() input = PdfFileReader(file('file.pdf', 'rb')) #create response object response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' # get number of pages num_pages = input.getNumPages() #register the font pdfmetrics.registerFont(TTFont('chinese_font','/usr/share/fonts/truetype/mac/LiHeiPro.ttf')) # generate watermark on the fly buffer = StringIO() # create string buffer for PDF pdf = Canvas(buffer) pdf.setFont("chinese_font", 12) pdf.setStrokeColorRGB(0.5, 1, 0) pdf.drawString(96, 26, "88888") pdf.save() # put on watermark from buffer watermark = PdfFileReader(buffer) page1 = input.getPage(0) page1.mergePage(watermark.getPage(0)) # add processed pdf page output.addPage(page1) #stream to browser outputStream = response output.write(response) outputStream.close() return response 
+6
source share

I'm not sure what I'm following. If you want the contents of the PDF to be sent to the browser, you must use an instance of HttpResponse . This line in your code

 outputStream = file("document-output.pdf", "wb") 

will not help to write the contents of the PDF in response. Instead, it seems to me that it will write the contents to a local file, which is not the same.

Update

Based on the comment:

How to send PDF content to an HttpResponse object since it will be opened in a browser, not as an attachment.

AFAIK (if anyone knows better, correct me), it depends on the browser.

If you do not specify Content-Disposition = "attachment; filename=foo.pdf from the response headers, you can send the contents to the browser without a specific file name. This caused my Firefox browser (3.6.10, Ubuntu Jaunty) to ask me if I want whether I open it using the program.In Chrome (6.0.472.62, Ubuntu Jaunty) the file loaded as download.pdf without any prompts.

+3
source share

remove "attachment" from this line with Chris comment

 response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' 
+1
source share

All Articles