2017-11-17 11 views
0
options = { 
    'page-size': 'Letter', 
    'margin-top': '0.75in', 
    'margin-right': '0.75in', 
    'margin-bottom': '0.75in', 
    'margin-left': '0.75in', 
    'encoding': "UTF-8", 
    'custom-header' : [ 
     ('Accept-Encoding', 'gzip') 
    ] 

} 

pdfkit.from_file('htmlfile.html', 'tjprofile.pdf', options=options) 

htmlファイルを特定の色のpdfに変更したいのですが、どうすればいいですか?Htmlファイルを特定の色のpdfに変換する

答えて

0
HTMLをPDFに変換する

使用BeautifulSoup

import sys 
    from bs4 import BeautifulSoup 
    from PDFWriter import PDFWriter 

def usage(): 
sys.stderr.write("Usage: python " + sys.argv[0] + " html_file pdf_file\n") 
sys.stderr.write("which will extract only the text from html_file and\n") 
sys.stderr.write("write it to pdf_file\n") 

def main(): 

# Create some HTML for testing conversion of its text to PDF. 
html_doc = """ 
<html> 
    <head> 
     <title> 
     Test file for HTMLTextToPDF 
     </title> 
    </head> 
    <body> 
    This is text within the body element but outside any paragraph. 
    <p> 
    This is a paragraph of text. Hey there, how do you do? 
    The quick red fox jumped over the slow blue cow. 
    </p> 
    <p> 
    This is another paragraph of text. 
    Don't mind what it contains. 
    What is mind? Not matter. 
    What is matter? Never mind. 
    </p> 
    This is also text within the body element but not within any paragraph. 
    </body> 
</html> 
""" 

pw = PDFWriter("HTMLTextTo.pdf") 
pw.setFont("Courier", 10) 
pw.setHeader("Conversion of HTML text to PDF") 
pw.setFooter("Generated by xtopdf: http://slid.es/vasudevram/xtopdf") 

# Use method chaining this time. 
for line in BeautifulSoup(html_doc).get_text().split("\n"): 
    pw.writeLine(line) 
pw.savePage() 
pw.close() 

if __name__ == '__main__': 
main() 
+0

私は、ファイルから変換したい@Parveenない文字列 –

+0

ではなく、HTMLコードを使用してから、開いて、HTMLファイルを読み込むためにオープン機能を使用し、保存すること値を変数に代入し、その変数をhtml_docの代わりに呼び出します。 –

+0

ubuntuユーザーはPDFWriterを利用できません –

関連する問題