如何将自定义字体添加到现有的PDF文件中?

huangapple go评论76阅读模式
英文:

How can I add custom font to an existing PDF file?

问题

I have a font file "myfont.otf". And I have a pdf file "target.pdf". I want to add the font file to the pdf file. I try to ask chatgpt.

The code is as follows:

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from fpdf import FPDF
from reportlab.lib.pagesizes import letter

packet = io.BytesIO()
pdf = FPDF()
pdf.add_page()

pdf.add_font('myfont', '', 'myfont.otf', uni=True)

pdf.set_font('myfont', '', 12)

pdf.text(10, 10, "Hello, world!")

pdf.output(packet, 'F')
packet.seek(0)

new_pdf = PdfFileReader(packet)

existing_pdf = PdfFileReader(open("target.pdf", "rb"))
output = PdfFileWriter()

page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)

outputStream = open("new_target.pdf", "wb")
output.write(outputStream)
outputStream.close()

But this code does not work correctly.

I am using the latest version of pypdf, and fpdf2.

英文:

I have a font file "myfont.otf". And I have a pdf file "target.pdf". I want to add the font file to the pdf file. I try to ask chatgpt.

The code is as follows:

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from fpdf import FPDF
from reportlab.lib.pagesizes import letter

packet = io.BytesIO()
pdf = FPDF()
pdf.add_page()

pdf.add_font('myfont', '', 'myfont.otf', uni=True)

pdf.set_font('myfont', '', 12)

pdf.text(10, 10, "Hello, world!")

pdf.output(packet, 'F')
packet.seek(0)

new_pdf = PdfFileReader(packet)

existing_pdf = PdfFileReader(open("target.pdf", "rb"))
output = PdfFileWriter()

page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)

outputStream = open("new_target.pdf", "wb")
output.write(outputStream)
outputStream.close()

But this code does not work correctly.

I am using the latest version of pypdf, and fpdf2.

答案1

得分: 0

Sure, here's the translated code:

import fitz  # PyMuPDF
myfont = "myfont.otf"

doc = fitz.open("input.pdf")
page = doc[pno]  # load page with number pno (0-based)
page.insert_font(fontname="F0", fontfile=myfont)

# now start inserting text on the page. Use insertion point (100,100) at first
page.insert_text((100,100), "Hello world", fontname="F0", fontsize=14, ...)

# some text in a box
rect = fitz.Rect(100, 120, 300, 200)  # a rectangle
page.insert_textbox(rect, "this is text in a rectangle with auto line breaks",
    fontname="F0", ...)
# etc.

# Before saving, optionally build a subset of myfont:
doc.subset_fonts()
doc.save("output.pdf", garbage=4, deflate=True)

If you have any more code to translate, please let me know.

英文:

How to add a custom font to a PDF using PyMuPDF.

import fitz  # PyMuDPDF
myfont = "myfont.otf"

doc = fitz.open("input.pdf")
page = doc[pno]  # load page with number pno (0-based)
page.insert_font(fontname="F0", fontfile=myfont)

# now start inserting text on the page. Use insertion point (100,100) at first
page.insert_text((100,100), "Hello world", fontname="F0", fontsize=14, ...)

# some text in a box
rect = fitz.Rect(100, 120, 300, 200)  # a rectangle
page.insert_textbox(rect, "this is text in a rectangle with auto line breaks",
    fontname="F0", ...)
# etc.

# Before saving, optionally build a subset of myfont:
doc.subset_fonts()
doc.save("output.pdf", garbage=4, deflate=True)

huangapple
  • 本文由 发表于 2023年4月13日 14:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定