Skip to content

Instantly share code, notes, and snippets.

@whosaysni
Created March 29, 2016 15:26
Show Gist options
  • Save whosaysni/9b209f67301dd9ffa682 to your computer and use it in GitHub Desktop.
Save whosaysni/9b209f67301dd9ffa682 to your computer and use it in GitHub Desktop.
解像度チェック用のPDFサンプルを生成する
# coding: utf-8
from reportlab.lib.pagesizes import A4
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate
from reportlab.lib.units import mm
from reportlab.platypus.flowables import PageBreak, Spacer
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(
TTFont('IPA Gothic', './IPAexfont00301/ipaexg.ttf'))
pdfmetrics.registerFont(
TTFont('IPA Mincho', './IPAexfont00301/ipaexm.ttf'))
styleSheet = getSampleStyleSheet()
MARGIN_SIZE = 25 * mm
PAGE_SIZE = A4
def create_pdfdoc(pdfdoc, story):
"""
Creates PDF doc from story.
"""
pdf_doc = BaseDocTemplate(pdfdoc, pagesize = PAGE_SIZE,
leftMargin = MARGIN_SIZE, rightMargin = MARGIN_SIZE,
topMargin = MARGIN_SIZE, bottomMargin = MARGIN_SIZE)
main_frame = Frame(MARGIN_SIZE, MARGIN_SIZE,
PAGE_SIZE[0] - 2 * MARGIN_SIZE, PAGE_SIZE[1] - 2 * MARGIN_SIZE,
leftPadding = 0, rightPadding = 0, bottomPadding = 0,
topPadding = 0, id = 'main_frame')
main_template = PageTemplate(id = 'main_template', frames = [main_frame])
pdf_doc.addPageTemplates([main_template])
pdf_doc.build(story)
from copy import deepcopy
text = (
"晩の7時15分少し前から Wilhelm Weber 町29番地の前の歩道を僕は行きつ"
"戻りつしていました."
"星の見えたのは近日珍らしいが,秋風が冷こくなってリンデの落葉が二ひら"
"三ひら散らばっているなどは誂向きの道具立です."
"其処で僕は或る Fräulein と rendez-vous があったのです.")
from string import printable
text2 = printable+''.join(unichr(u_ord) for u_ord in range(0x2f58, 0x2fd5))
def demo():
"""
Runs demo demonstrating usage of Spreadsheet Tables.
"""
story = []
for fn in ['Gothic', 'Mincho']:
for fs in [8, 10, 12, 14, 16, 18, 24, 28, 32, 36, 42, 48][:5]:
style = deepcopy(styleSheet['BodyText'])
style.fontSize = fs
style.leading = max(int(fs*1.1), fs+1)
style.fontName = 'IPA '+fn
style.wordWrap = 'CJK'
story.append(Paragraph('%d pt --- '%(fs)+text, style))
style = deepcopy(styleSheet['BodyText'])
style.fontSize = 10
style.leading = 11
style.wordWrap = 'CJK'
style.fontName = 'IPA Gothic'
story.append(Paragraph(text2, style))
style.fontSize = 10
style.leading = 11
style.fontName = 'IPA Mincho'
style.wordWrap = 'CJK'
story.append(Paragraph(text2, style))
create_pdfdoc('jplorem.pdf', story)
if __name__ == '__main__':
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment