昨天的成果輸出後,發現文字是中文的地方還是黑色方塊,
再用其它的方法。
這邊使用文件內建 HeiseiKakuGo-W5
導入函式的地方註冊
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
def save_as_pdf(filename, text):
這邊使用 macOS 內建 PingFang.ttc
font_path = "/System/Library/Fonts/Supplemental/PingFang.ttc"
if os.path.exists(font_path):
try:
pdfmetrics.registerFont(TTFont("PingFang", font_path))
font_name = "PingFang"
except Exception:
若註冊失敗,改用內建字體 HeiseiKakuGo-W5
pdfmetrics.registerFont(UnicodeCIDFont("HeiseiKakuGo-W5"))
font_name = "HeiseiKakuGo-W5"
else:
以上沒有 PingFang 就直接用內建 HeiseiKakuGo-W5
pdfmetrics.registerFont(UnicodeCIDFont("HeiseiKakuGo-W5"))
font_name = "HeiseiKakuGo-W5"
再次運行
下面能看到輸出pdf成功,也能看到文字成功顯示