AI教程网 - 未来以来,拥抱AI;新手入门,从AI教程网开始......

[286]python将html转化为pdf

html转化为pdf AI君 67℃

前言

前面我们对博客园的文章进行了爬取,结果比较令人满意,可以一下子下载某个博主的所有文章了。但是,我们获取的只有文章中的文本内容,并且是没有排版的,看起来也比较费劲。。。

咋么办的?一个比较好的方法是将文章的正文内容转化成pdf,就不要考虑排版的事情了,看起来比较美观,也不会丢失一些关键信息。

python中将html转化为pdf的常用工具是Wkhtmltopdf工具包,在python环境下,pdfkit是这个工具包的封装类。如何使用pdfkit以及如何配置呢?分如下几个步骤。

1、下载wkhtmltopdf安装包,并且安装到电脑上,在系统Path变量中添加wkhtmltopdf的bin路径,以便于pdfkit的调用。
下载地址:https://wkhtmltopdf.org/downloads.html
请根据自己的系统版本,选择合适的安装包。如果没有装C语言库,建议选择Windows下的第二种。
在这里插入图片描述
2、在pycharm中安装pdfkit库,过程就不介绍啦,前面讲过类似的内容。

pip install pdfkit

3、在pycharm中安装whtmltopdf库。

pip install wkhtmltopdf

这个和第一步中的安装包是两个东西,请区别开来。

用法简介

对于简单的任务来说,代码很easy,比如:

import pdfkit
pdfkit.from_url('http://baidu.com','out.pdf')
pdfkit.from_file('test.html','out.pdf')
pdfkit.from_string('Hello!','out.pdf')

pdfkit包含的方法很少,主要用的就是这三个,我们简单看一下每个函数的API:

from_ulr()

def from_url(url, output_path, options=None, toc=None, cover=None,
             configuration=None, cover_first=False):
    """
    Convert file of files from URLs to PDF document

    :param url: url可以是某一个url也可以是url的列表,
    :param output_path: 输出pdf的路径,如果设置为False意味着返回一个string

    Returns: True on success
    """

    r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
               configuration=configuration, cover_first=cover_first)

    return r.to_pdf(output_path)

from_file()

def from_file(input, output_path, options=None, toc=None, cover=None, css=None,
              configuration=None, cover_first=False):
    """
    Convert HTML file or files to PDF document

    :param input: 输入的内容可以是一个html文件,或者一个路径的list,或者一个类文件对象
    :param output_path: 输出pdf的路径,如果设置为False意味着返回一个string

    Returns: True on success
    """

    r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css,
               configuration=configuration, cover_first=cover_first)

    return r.to_pdf(output_path)

from_string()

def from_string(input, output_path, options=None, toc=None, cover=None, css=None,
                configuration=None, cover_first=False):
    #类似的,这里就不介绍了
    r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css,
               configuration=configuration, cover_first=cover_first)
    return r.to_pdf(output_path)

举几个栗子

我们可以传入列表:

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,可以将其读取成一个变量,其实就是一个string变量。

# Use False instead of output path to save pdf to a variable
pdf = pdfkit.from_url('http://google.com', False)

指定pdf的格式

我们可以指定各种选项,就是上面三个方法中的options。
具体的设置可以参考https://wkhtmltopdf.org/usage/wkhtmltopdf.txt 里面的内容。
我们这里只举个栗子:

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')
    ]
    'cookie': [
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    ],
    'no-outline': None
}

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

默认的,pdfkit会show出所有的output,如果你不想使用,可以设置为quite:

options = {
    'quiet': ''
    }

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

我们还可以传入任何html标签,比如:

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

改进

有了上面的知识之后,我们大可以尝试一下,如果将之前的save_file方法做一些改变,就能够实现我们下载PDF的目标啦。
我们将方法名改成save_to_pdf,并且在get_body方法中直接返回str(div),而不是div.text。代码如下:

def save_to_pdf(url):
    '''
    根据url,将文章保存到本地
    :param url:
    :return:
    '''
    title=get_title(url)
    body=get_Body(url)
    filename=author+'-'+title+'.pdf'
    if '/' in filename:
        filename=filename.replace('/','+')
    if '\\' in filename:
        filename=filename.replace('\\','+')
    print(filename)
    options = {
        'page-size': 'Letter',
        'encoding': "UTF-8",
        'custom-header': [
            ('Accept-Encoding', 'gzip')
        ]
    }
    #本来直接调用pdfkid的from方法就可以了,但是由于我们的wkhtmltopdf安装包有点问题,一直没法搜到,所以只能用本办法,直接配置了wk的地址
    #尴尬了,主要是一直没法下载到最新的wk,只能在网上down了旧版本的。有谁能下到的话发我一份。。。
    config=pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
    pdfkit.from_string(body,filename,options=options,configuration=config)
    print('打印成功!')

在这里插入图片描述
哈哈,成功了,下载了这么多pdf,回头慢慢看就可以了。


常见错误:

IOError: No wkhtmltopdf executable found:

python使用pdfkit中,如果使用pdfkit.from_url 或者pdfkit.from_string等,就会出现上述错误。而且如果你使用pip安装了 wkhtmltopdf,还是会出现这个问题:
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf -
因此需要去安装windows版本的wkhtmltopdf
此处进入下载网址
安装完成之后需要在代码中添加以下内容:

path_wk = r'C:\Python27\wkhtmltopdf\bin\wkhtmltopdf.exe' #安装位置
config = pdfkit.configuration(wkhtmltopdf = path_wk)
pdfkit.from_url(url, 'taobao.pdf', configuration=config)

IOError: ‘Command Failed’

如果出现这个错误意味着 PDFKit不能处理一个输入。你可以尝试直接在错误信息后面直接运行一个命令来查看是什么导致了这个错误 (某些版本的 wkhtmltopdf会因为段错误导致处理失败

===============================================
1.首先,使用谷歌浏览器打开要保存的页面,例如本人打开了学习python的一个网页。
在这里插入图片描述
2. 在浏览器右上角,点击“自定义及控制Google Chrome”标签,在弹出的下拉菜单中选择“打印”;或使用快捷键“Crtl+P”调出此菜单。
在这里插入图片描述
3. 在弹出的打印设置及预览页面,在页面右侧的“目标打印机”选项处点击“更改”,默认为设置连接的实际打印机。
在这里插入图片描述
4. 在弹出的“选择打印机”窗口,在本地目标打印机的管理框里面选择“另存为PDF”选项。
在这里插入图片描述
5. 之后还可以具体设置打印的细节,如“页码“,”布局“,”纸张尺寸“,”边距“和”选项“的设置。
在这里插入图片描述
6. 设置好之后选择”保存“按钮,在弹出的窗口填写好文件名称,就可以保存了。
在这里插入图片描述

参考:https://www.cnblogs.com/xingzhui/p/7887212.html
https://blog.csdn.net/sinat_20280061/article/details/51674605
https://jingyan.baidu.com/article/546ae1853b1eb51148f28c6e.html

作者:周小董
原文链接:https://blog.csdn.net/xc_zhou/article/details/80952168

转载请注明:www.ainoob.cn » [286]python将html转化为pdf

喜欢 (0)or分享 (0)