【问题标题】:How to convert a website to pdf with cheerio and fs如何使用cheerio和fs将网站转换为pdf
【发布时间】:2021-01-20 16:38:38
【问题描述】:

我正在尝试从网站获取 pdf 文档,但我一直在获取 pdf 上的代码,而不是图片或它的“屏幕截图”。我想坚持cheerio和fs。

这是它保存的地方。

const pdf_completo = fs.createWriteStream('paginas/pdf_completo.pdf');

这是我试图用来获取它的函数。

async function obtener_pdf_pagina(){
    const $ = await request({
        uri: 'http://quotes.toscrape.com/',
        transform: pdf => cheerio.load(pdf)
    });
    const todo_pagina = $('*');
    pdf_completo.write(todo_pagina.html());
}

【问题讨论】:

  • 此代码中的任何内容都不会创建网站的屏幕截图或图像。您所做的只是将原始 HTML 放入您的文件中。您将需要使用可以制作网站屏幕截图的库或可以将 HTML 转换为 PDF 或类似内容的库。
  • 所以不能用cheerio来做这个?

标签: javascript node.js fs cheerio fs-extra


【解决方案1】:

我不认为cheerio 有任何截图功能。 Cheerio 解析 HTML 并为您提供可以与之交互的类似 DOM 的 API,但实际上不会在屏幕上呈现任何内容。

据我所知,你需要像 puppeteer 这样的东西来做截图(它使用 chromium 浏览器引擎来渲染)

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 2015-11-10
    • 2014-06-15
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多