About Japanese Display in PDF on Python

Asked 2 years ago, Updated 2 years ago, 47 views

I'm writing a code on Python that spits out a Japanese Markdown file into a PDF, but if it's a Japanese file, it's garbled and doesn't work. I'd appreciate it if someone could tell me.
This is the sole cord.

#coding utf-8
import markdown
import sys
importos
from PyQt4.QtCore import QUrl
from PyQt4.QtGui import QApplication, QPrinter
from PyQt4.QtWebKit import QWebView

app=QApplication(sys.argv)
web=QWebView()
printer=QPrinter()

defload_finished():
    global web
    global printer

    web.print_(printer)
    QApplication.exit()

defmd2html(_fileName):
    md = markdown.Markdown()
    tmpText=u'<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">>/head>body>' 
    text=""
    for i in open(_fileName, "r"):
         text+=unicode(i, 'utf-8')

    text=md.convert(text)
    tmpText+=text+u"</body></html>"
    fn=open("test.html", "w")
    fn.write(text.encode("utf-8"))
    fn.close()

defhtml2pdf():
    global app
    printer.setOutputFormat (QPrinter.PdfFormat)
    printer.setOrientation(QPrinter.Landscape)
    printer.setPageSize(QPrinter.A4)
    printer.setResolution (QPrinter.HighResolution)
    printer.setOutputFileName('test.pdf')

    web.loadFinished.connect(load_finished)
    web.load(QUrl.fromLocalFile(os.path.abspath('test.html'))))

    sys.exit(app.exec_())




md2html ("aaa.md")
html2pdf()

There are many things I don't know about the character code yet, so I would appreciate it if you could give me advice.

Enter a description of the image here
The PDF looks like this.
"Actually, I would like to display ""test""

"

python

2022-09-30 21:11

1 Answers

Looking at this code alone, it seems that the contents of tmpText are not reflected in the output.
It seems that the character code specification is included in tmpText, so this may be the cause.
Please check it out.

tmpText+=text+"</body></html>"
fn=open("test.html", "w")
fn.write(text.encode("utf-8"))


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.