python
,pdf
最近修改日期:20201224
# 產生PDF方法
# 感覺是畫圖後,再產生PDF。
# 如何畫表格?如何換行,如何畫圖型?
# W H 的意義?=>W:幾個字元寬,H:幾行高
import os
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
def make_pdf(ps,W,H,fn="output.pdf"):
#use reportlab to generate a pdf with the prime in it
end = " is a prime, with %i digits." % (W*H)
rows = []
for i in range(H):
rows.append( ps[i*W: i*W+W])
# print(rows)
c = canvas.Canvas(fn)
# 由C:\Windows\Fonts\微軟正黑體 目錄下,將msjh.ttc 複製在與程式同目錄下。
# pdfmetrics.registerFont(TTFont('msjh', 'msjh.ttc'))
# 字型放於c:\Windows\Fonts資料夾下的 msjh.ttc (細明體)
Fontfile = os.path.join('c:\\', 'Windows', 'Fonts','msjh.ttc')
pdfmetrics.registerFont(TTFont('msjh', Fontfile))
c.setPageSize((16*max(W,len(end)), 16*(2+H)))
print (16*max(W,len(end)), 16*(2+H))
c.setFont('msjh', 16)
TOP = 16*(1+H)
for i,r in enumerate(rows):
c.drawString(0, TOP - i*16, r)
c.drawString(0, 8, end)
c.save()
myStr = '這是Python產生PDF方法'
make_pdf('aa',3,4,'MyPdf.pdf')
# 20200526 By YTC
參考:Python reportlab.pdfbase.ttfonts.TTFont() Examples Example 7
M20201224 By YTC 修正無法產出PDF的Bug
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing