views:

81

answers:

2

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()
+1  A: 

I am not sure I follow. If you want the PDF content to be sent to the browser you should use an HttpResponse instance. This line in your code

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

will not serve to write the PDF contents to the response. Instead it looks to me like it will write the contents to a local file, which is not the same.

Update

Based on comment:

How to send PDF content to a HttpResponse object as it will open in the browser, not as an attachment.

AFAIK (if anyone knows better, correct me) this is browser dependent.

If you leave out the Content-Disposition = "attachment; filename=foo.pdf from the response headers you can send the contents to the browser without a specific filename. This prompted my Firefox browser (3.6.10, Ubuntu Jaunty) to ask me if I wanted to open it using a program. On Chrome (6.0.472.62, Ubuntu Jaunty) the file got downloaded as download.pdf without any prompting.

Manoj Govindan
Yes, I know. Actually that's what I am asking for:) How to send PDF content to a HttpResponse object as it will open in the browser, not as an attachment.
israkir
@israkir: Updated my answer. See above.
Manoj Govindan
A: 

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.

anand