RSS Feed

RSS

Comments RSS

Getting aquainted with the M1911

This is a different sort of blog post for me.  No code snippets or anything like that.   I have recently become the owner of a handgun, a M1911-A1 CS, .45 cal.  It is the short barrel, 3.5″, version of the classic M1911-A1.  While I have had other types of firearms all my life:  shotguns and riffles, this is the first time I have owned a handgun.   I guess I was waiting for the kids to grow up and basically be out of the house?  Not sure if that was really the reason, but I do know that I really like this gun!  It feels great in my hand and is a size that will simplify concealed carry capabilities.  Today I went out shooting with friends who who are quite a bit more experienced with handguns than I am, one of which is a veteran with combat experience.  I sure appreciated the opportunity to learn and improve my shooting.   It was also a lot of fun!  I am going to have to do more of this.

Python, watermark a PDF

This blog entry shows how to use Python and two third party modules (pyPdf and ReportLab) to watermark a PDF.

#This sample uses two third part modules for Python, 
#pyPdf & ReportLab to achieve creating and placing 
#watermark text at angle on an existing PDF file. 
#This example was produced with Python 2.7 
#See http://pybrary.net/pyPdf for more informaton about pyPdf. 
#See http://www.reportlab.com for more information about ReportLab. 

#Import the needed external modules and functions from pyPdf and reportlab.
from pyPdf import PdfFileWriter, PdfFileReader 
from reportlab.pdfgen import canvas

#Use reportlab to create a PDF that will be used 
#as a watermark on another PDF.
c= canvas.Canvas("watermark.pdf") 
c.setFont("Courier", 60)
#This next setting with make the text of our 
#watermark gray, nice touch for a watermark.
c.setFillGray(0.5,0.5)
#Set up our watermark document. Our watermark 
#will be rotated 45 degrees from the direction 
#of our underlying document.
c.saveState() 
c.translate(500,100) 
c.rotate(45) 
c.drawCentredString(0, 0, "A WATERMARK!") 
c.drawCentredString(0, 300, "A WATERMARK!") 
c.drawCentredString(0, 600, "A WATERMARK!") 
c.restoreState() 
c.save() 

#Read in the PDF that will have the PDF applied to it.
output = PdfFileWriter() 
input1 = PdfFileReader(file("original_pdf.pdf", "rb")) 

#Just to demo this function from pyPdf. 
#If the PDF has a title, this will print it out.
print "title = %s" % (input1.getDocumentInfo().title)

#Open up the orgininal PDF.
page1 = input1.getPage(0)

#Read in the file created above by ReportLab for our watermark.
watermark = PdfFileReader(file("watermark.pdf", "rb"))
#Apply the watermark by merging the two PDF files.
page1.mergePage(watermark.getPage(0))
#Send the resultant PDF to the output stream.
output.addPage(page1)

#Just to demo this function from pyPdf. 
#Return the number of pages in the watermarked PDF.
print "watermarked_pdf.pdf has %s pages." % input1.getNumPages()

#write the output of our new, watermarked PDF.
outputStream = file("watermarked_pdf.pdf", "wb") 
output.write(outputStream) 
outputStream.close()