3

How to change the font in pyPDF 2 module.

I tried print(help(canvas.Canvas))

I tried the initialFontName = None and initialFontSize = None

but my text didn't change. Also, I'm doing this using raspberry-pi with raspbian operating system.

Here's my code

        can = canvas.Canvas(packet, pagesize=letter, initialFontName = 'Helvetica', initialFontSize = 5)
        now = datetime.datetime.now()

        can.drawString(250, 500, self.get_Firstn1 + ' ' + self.get_Middlen1 +' ' + self.get_Lastn1)
        can.drawString(300, 550, "Hello World")
        can.drawString(350, 600,"Hello")

        can.save()

        packet.seek(0)
        new_pdf = PdfFileReader(packet)


        existing_pdf = PdfFileReader(open("Sample.pdf", "rb"))
        existing_pdf.decrypt('')
        output = PdfFileWriter()
            # add the "watermark" (which is the new pdf) on the existing page

        page = existing_pdf.getPage(0)
        page.mergePage(new_pdf.getPage(0))

        output.addPage(page)

        #write "output" to a real file
        outputStream = open("destination.pdf", "wb")
        output.write(outputStream)
        outputStream.close()
            #open pdf file
        opener ="open" if sys.platform == "darwin"else "xdg-open"
        subprocess.call([opener, "destination.pdf"])

I tried to change fonts like Arial, Times New Roman, Cambria etc. but it only accepts Helvetica and the text size and font didn't change.

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
tin tinie
  • 63
  • 1
  • 8

0 Answers0