import pdfkit
import pymongo

'''将字符串生成pdf文件'''
def str_to_pdf(string, to_file):
    # 将wkhtmltopdf.exe程序绝对路径传入config对象
    path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    options = {
        'page-size': 'Letter',
        'margin-top': '0.75in',
        'margin-right': '0.75in',
        'margin-bottom': '0.75in',
        'margin-left': '0.75in',
        'encoding': "UTF-8",
        'minimum-font-size':"25",
        'no-outline': None
    }
    # 生成pdf文件，to_file为文件路径
    pdfkit.from_string(string, to_file, configuration=config,options=options)
    print('完成')

# myclient = pymongo.MongoClient("mongodb://localhost:27017/")
# mydb = myclient["cnnStories"]
# mycol = mydb["stories"]
 
# x = mycol.find_one()
# # 正文x["content"]
# mystr="<br>".join(x["content"])
# x = mycol.find({"_id":1})
# for i in x:
#     print(i["highlight"])
# print(x)

# str_to_pdf(mystr,'0.pdf')
str = ''
with open('result2.txt', 'r', encoding='utf-8') as f:
    fl = 1
    for line in f.readlines():
        line = line.strip()
        if (not fl):
            str += ' '
        str += line
        fl = 0
        if (line == ''):
            fl = 1
            str += '<br>'
print(str)
str_to_pdf(str, 'test2.pdf')