46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
|
from wkhtmltopdf.views import PDFTemplateResponse
|
||
|
from PyPDF2 import PdfFileWriter, PdfFileReader
|
||
|
from django.test.client import RequestFactory
|
||
|
from django.conf import settings
|
||
|
import StringIO
|
||
|
|
||
|
def generate_pdf_letter(template, formatdict):
|
||
|
### produce text-only PDF from template
|
||
|
pdfgenerator = PDFTemplateResponse(
|
||
|
request=RequestFactory().get('/'),
|
||
|
template=template,
|
||
|
context=formatdict,
|
||
|
cmd_options={
|
||
|
'margin-top': 40,
|
||
|
'margin-bottom': 50,
|
||
|
},
|
||
|
)
|
||
|
textonlypdf = StringIO.StringIO()
|
||
|
textonlypdf.write(pdfgenerator.rendered_content)
|
||
|
|
||
|
### create a blank pdf to work with
|
||
|
finalpdf = PdfFileWriter()
|
||
|
|
||
|
### open the text-only pdf
|
||
|
pdfreader = PdfFileReader(textonlypdf)
|
||
|
|
||
|
### get watermark from watermark file
|
||
|
watermark = PdfFileReader(open(settings.LETTERHEAD_PDF_PATH, 'rb'))
|
||
|
|
||
|
### add the watermark to all pages
|
||
|
for pagenum in xrange(pdfreader.getNumPages()):
|
||
|
page = pdfreader.getPage(pagenum)
|
||
|
try:
|
||
|
page.mergePage(watermark.getPage(0))
|
||
|
except ValueError:
|
||
|
### watermark pdf might be broken?
|
||
|
return False
|
||
|
### add page to output
|
||
|
finalpdf.addPage(page)
|
||
|
|
||
|
### return a file object with the data
|
||
|
returnfile = StringIO.StringIO()
|
||
|
finalpdf.write(returnfile)
|
||
|
return returnfile
|
||
|
|