Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a generated PDF file in browser?

I have written a Pdf merger which merges an original file with a watermark.

What I want to do now is to open 'document-output.pdf' file in the browser by a Django view. I already checked Django's related articles, but since my approach is relatively different, I don't directly create the PDF object, using the response object as its "file.", so I am kind of lost.

So, how can I do is 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()
like image 267
israkir Avatar asked Sep 22 '10 08:09

israkir


People also ask

How can I open PDF in browser without downloading?

However, DocFly's PDF file opener provides another option for viewing PDF files without having to download any software. With our free PDF viewer online users can view PDF files from any computer with an Internet connection.

How do I open a PDF directly in Chrome?

To use the extension, right-click on a PDF link and select the "Open in PDF viewer" item. The extension downloads the PDF file in your default downloads directory and then executes the external command to open the downloaded file with the default PDF viewer (e.g. Adobe Acrobat Reader or FoxIt PDF reader).


2 Answers

I know its an older post but we can use the embed tag of html to implement this kind of functionality. For e.g.:

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

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

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

and in the abc.html you can put this filename (with the path) in the embed tag, as mentioned above.

Hope this helps.

like image 78
Ankit Jaiswal Avatar answered Oct 20 '22 03:10

Ankit Jaiswal


In addition to sending your PDF back to the browser, you can also save some cycles by storing your 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
like image 29
Chris Avatar answered Oct 20 '22 05:10

Chris