python
,pdf
# 產生PDF方法二
import reportlab
import os
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# 用Canvas建立pdf畫布
canv = canvas.Canvas("fonts_demo.pdf")
# 載入字型檔 (放置於 c:\Windows\Fonts 資料夾下 )
# msjhbd.ttc 微軟正黑體 粗體
# msjhl.ttc 微軟正黑體 淡
# msjh.ttc 微軟正黑體 標準
font_variants = ('msjh','msjhl','msjhbd')
folder = os.path.join('c:\\', 'Windows', 'Fonts')
for variant in font_variants:
pdfmetrics.registerFont(TTFont(variant, os.path.join(folder, variant+'.ttc')))
# 0,0 座標為頁的左側底端。
# 每英吋72點,所以在8.5英寸的頁面,頂端座標為8.5*72
# canvas 實際產生一個A4的頁面。這樣的設定已經很接近。
# x = 0; y = 8.5 * 72
# canv.setFont('msjhbd', 30)
# canv.drawString(x,y,"This is bold 這是粗體字")
x = 0; y = 826 # 297/25.4*72 - 16
canv.setFont('msjhbd', 16)
canv.drawString(x+16*2,y,"This is bold 這是粗體字")
# 往下移動一吋(72點),再畫出下一行。
# y = y - 72
y = y - 30
canv.setFont('msjhl', 30)
canv.drawString(x,y," 這是淡體字")
# 再往下一行:
y = y - 30
canv.setFont('msjh', 30)
canv.drawString(x,y,"這是標準正黑體")
# 輸出結果:Save your work
canv.save()
# 紙張資訊
# A4紙尺寸是210毫米(寬)×297毫米(高),而1英寸=2.54厘米
# 當解析度是72像素/英寸時,A4紙像素長寬分別是842×595;
# 當解析度是120像素/英寸時,A4紙像素長寬分別是2105×1487;
# S20200526 By YTC 依據參考改寫
參考:reportlab-在我的pdf中,我想創建粗體和普通文本
參考: 國際A4紙尺寸大小是多少?標準A4紙像素解析度換算方法
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