Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 精品午夜寂寞黄网站在线,国产亚洲精品日韩综合网,日韩午夜中文字幕电影

          整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          使用Python將HTML轉(zhuǎn)成PDF

          要使用的是wkhtmltopdf的Python封裝——pdfkit

          安裝

          1. Install python-pdfkit:

          $ pip install pdfkit

          2. Install wkhtmltopdf:

          • Debian/Ubuntu:

          $ sudo apt-get install wkhtmltopdf

          • Redhat/CentOS

          sudo yum intsall wkhtmltopdf

          • MacOS

          brew install Caskroom/cask/wkhtmltopdf

          使用

          一個簡單的例子:

          import pdfkit

          pdfkit.from_url('http://google.com', 'out.pdf')

          pdfkit.from_file('test.html', 'out.pdf')

          pdfkit.from_string('Hello!', 'out.pdf')

          你也可以傳遞一個url或者文件名列表:

          pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')

          pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')

          也可以傳遞一個打開的文件:

          with open('file.html') as f:

          pdfkit.from_file(f, 'out.pdf')

          如果你想對生成的PDF作進(jìn)一步處理, 你可以將其讀取到一個變量中:

          # 設(shè)置輸出文件為False,將結(jié)果賦給一個變量

          pdf = pdfkit.from_url('http://google.com', False)

          你可以制定所有的 wkhtmltopdf 選項 http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 你可以移除選項名字前面的 '--' .如果選項沒有值, 使用None, False or * 作為字典值:

          options = {

          'page-size': 'Letter',

          'margin-top': '0.75in',

          'margin-right': '0.75in',

          'margin-bottom': '0.75in',

          'margin-left': '0.75in',

          'encoding': "UTF-8",

          'no-outline': None

          }

          pdfkit.from_url('http://google.com', 'out.pdf', options=options)

          默認(rèn)情況下, PDFKit 將會顯示所有的 wkhtmltopdf 輸出. 如果你不想看到這些信息,你需要傳遞一個 quiet 選項:

          options = {

          'quiet': ''

          }

          pdfkit.from_url('google.com', 'out.pdf', options=options)

          由于wkhtmltopdf的命令語法 , TOC 和 Cover 選項必須分開指定:

          toc = {

          'xsl-style-sheet': 'toc.xsl'

          }

          cover = 'cover.html'

          pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)

          當(dāng)你轉(zhuǎn)換文件、或字符串的時候,你可以通過css選項指定擴(kuò)展的 CSS 文件。

          # 單個 CSS 文件

          css = 'example.css'

          pdfkit.from_file('file.html', options=options, css=css)

          # Multiple CSS files

          css = ['example.css', 'example2.css']

          pdfkit.from_file('file.html', options=options, css=css)

          你也可以通過你的HTML中的meta tags傳遞任意選項:

          body = """

          <html>

          <head>

          <meta name="pdfkit-page-size" content="Legal"/>

          <meta name="pdfkit-orientation" content="Landscape"/>

          </head>

          Hello World!

          </html>

          """

          pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape

          配置

          每個API調(diào)用都有一個可選的參數(shù)。這應(yīng)該是pdfkit.configuration()API 調(diào)用的一個實(shí)例. 采用configuration 選項作為初始化參數(shù)。可用的選項有:

          • wkhtmltopdf ——wkhtmltopdf二進(jìn)制文件所在的位置。默認(rèn)情況下pdfkit 會嘗試使用which (在類UNIX系統(tǒng)中) 或 where (在Windows系統(tǒng)中)來判斷
          • meta_tag_prefix -- pdfkit的前綴指定 meta tags(元標(biāo)簽) - 默認(rèn)情況是pdfkit-

          示例 :針對wkhtmltopdf不在系統(tǒng)路徑中(不在$PATH里面)

          PATH里面):

          config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))

          pdfkit.from_string(html_string, output_file, configuration=config)

          問題

          IOError:'No wkhtmltopdf executable found':

          確保 wkhtmltopdf 在你的系統(tǒng)路徑中(PATH), 會通過 configuration進(jìn)行了配置 (詳情看上文描述)。 在Windows系統(tǒng)中使用where wkhtmltopdf命令 或 在 linux系統(tǒng)中使用 which wkhtmltopdf 會返回 wkhtmltopdf二進(jìn)制可執(zhí)行文件所在的確切位置.

          • IOError: 'Command Failed'

          如果出現(xiàn)這個錯誤意味著 PDFKit不能處理一個輸入。你可以嘗試直接在錯誤信息后面直接運(yùn)行一個命令來查看是什么導(dǎo)致了這個錯誤 (某些版本的 wkhtmltopdf會因為段錯誤導(dǎo)致處理失敗)

          • 正常生成,但是出現(xiàn)中文亂碼

          確保兩項:

          1)、你的系統(tǒng)中有中文字體

          2)、在html中加入

          下面是我隨便寫的一個HTML表格:

          <html>

          <head><meta charset="UTF-8"></head>

          <body>

          <table width="400" border="1">

          <tr>

          <th align="left">Item....</th>

          <th align="right">1</th>

          </tr>

          <tr>

          <td align="left">衣服</td>

          <td align="right">1.10</td>

          </tr>

          <tr>

          <td align="left">化妝品</td>

          <td align="right">.00</td>

          </tr>

          <tr>

          <td align="left">食物</td>

          <td align="right">0.40</td>

          </tr>

          <tr>

          <th align="left">tOTAL</th>

          <th align="right">01.50</th>

          </tr>

          </table>

          </body>

          </html>

          下面是生成的PDF截圖

          一篇[73、Spring Boot AntLib 模塊]

          下一篇[76、Spring Boot 應(yīng)用程序]

          一篇[64、安裝 SpringBoot 應(yīng)用程序]

          下一篇[67、使用 CLI]


          主站蜘蛛池模板: 国偷自产Av一区二区三区吞精| 中文字幕av无码一区二区三区电影| 三上悠亚一区二区观看| 国产精品 一区 在线| 伊人色综合网一区二区三区 | 91精品国产一区| 久久亚洲中文字幕精品一区四| 国产一区二区三区小向美奈子 | 国产一区二区三区福利| 日韩免费观看一区| 久久婷婷色综合一区二区| 国产在线观看一区二区三区| 91精品乱码一区二区三区| 人体内射精一区二区三区| 亚洲AV无码一区二区乱孑伦AS| 中文字幕一区二区区免| 国产福利电影一区二区三区,亚洲国模精品一区 | 天堂资源中文最新版在线一区 | 无码国产精品一区二区免费式影视 | 日韩免费观看一区| 国产亚洲一区二区三区在线观看 | 国产一区二区三区露脸| 午夜AV内射一区二区三区红桃视 | 久久久无码精品国产一区| 无码av免费一区二区三区| 国99精品无码一区二区三区 | 亚洲综合一区国产精品| 无码国产精品一区二区免费I6| 国产精品视频一区二区猎奇| 在线欧美精品一区二区三区| 亚洲熟女综合一区二区三区| 国产精品无码一区二区三区不卡| 高清精品一区二区三区一区| 色婷婷av一区二区三区仙踪林| 亚洲高清一区二区三区电影| 国产视频一区二区| 国产免费播放一区二区| 国产成人无码AV一区二区| 人妻夜夜爽天天爽爽一区| 无码喷水一区二区浪潮AV| 一区二区三区免费精品视频|