Created
January 3, 2019 08:14
-
-
Save tatsu38/c76bbecfedab08803347c97537ca1761 to your computer and use it in GitHub Desktop.
addPageNumberToPDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| import reportlab | |
| from reportlab.lib.units import mm | |
| from reportlab.pdfbase import pdfmetrics | |
| from reportlab.pdfbase.ttfonts import TTFont | |
| from reportlab.pdfgen import canvas | |
| from PyPDF2 import PdfFileWriter, PdfFileReader | |
| from io import BytesIO | |
| class PagePdf(): | |
| fontName = '' | |
| fontFile = '' | |
| _fontSize = 8 | |
| _margin = 10 | |
| beforeStr = '―― ' | |
| afterStr = ' ――' | |
| def __init__(self, fontName, fontFile): | |
| self.fontName = fontName | |
| self.fontFile = fontFile | |
| @property | |
| def fontSize(self): | |
| return self._fontSize | |
| @fontSize.setter | |
| def fontSize(self, value): | |
| self._fontSize = float(value) | |
| @property | |
| def margin(self): | |
| return self._margin | |
| @margin.setter | |
| def margin(self, value): | |
| self._margin = float(value) | |
| def convert(self, inFilePath, outFilePath): | |
| pdfmetrics.registerFont(TTFont(self.fontName, self.fontFile)) | |
| outPdf = PdfFileWriter() | |
| with open(inFilePath, 'rb') as f: | |
| inPdf = PdfFileReader(f,strict=False) | |
| for i in range(inPdf.getNumPages()): | |
| print('page: %d'%(i)) | |
| inPage = inPdf.getPage(i) | |
| packet = self.createPagePdf(i + 1, inPage.cropBox.getWidth(), inPage.cropBox.getHeight()) | |
| numberPdf = PdfFileReader(packet) | |
| numberPage = numberPdf.getPage(0) | |
| inPage.mergePage(numberPage) | |
| outPdf.addPage(inPage) | |
| with open(outFilePath, 'wb') as f: | |
| outPdf.write(f) | |
| def createPagePdf(self, num, width, height): | |
| print(str(width) + " " + str(height)) | |
| packet = BytesIO() | |
| c = canvas.Canvas(packet) | |
| c.setPageSize((width, height)) | |
| c.setFont(self.fontName, self.fontSize) | |
| c.drawCentredString(int(width / 2), (self.margin)*mm, self.beforeStr + str(num) + self.afterStr) | |
| c.showPage() | |
| c.save() | |
| packet.seek(0) | |
| return packet | |
| if __name__ == "__main__": | |
| import sys, os | |
| if len(sys.argv) < 2 or not os.path.isfile(sys.argv[1]): | |
| print('Error: not input file', file=sys.stderr) | |
| sys.exit(1) | |
| if len(sys.argv) < 3: | |
| print('Error: not output file', file=sys.stderr) | |
| sys.exit(1) | |
| if len(sys.argv) < 4: | |
| print('Error: not font name', file=sys.stderr) | |
| sys.exit(1) | |
| if len(sys.argv) < 5: | |
| print('Error: not font file', file=sys.stderr) | |
| sys.exit(1) | |
| pagePdf = PagePdf(sys.argv[3], sys.argv[4]) | |
| if 6 <= len(sys.argv): | |
| pagePdf.fontSize = sys.argv[5] | |
| if 7 <= len(sys.argv): | |
| pagePdf.margin = sys.argv[6] | |
| pagePdf.convert(sys.argv[1], sys.argv[2]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ./addPageNumberToPDF.py 'body.pdf' 'body2.pdf' 'GenYoMinJP-Light' 'GenYoMinJP-Light.ttf' 7.11 10.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment