在 pdf puppeteer Nodejs 上允许多种语言

问题描述 投票:0回答:3

我正在使用 puppeteer 从服务器端生成 pdf,我们在服务器 api 上传递 Angular 应用程序 Web url,它将返回 pdf。 一切都运行得很顺利,但我们的 pdf 文件有两种语言。它将支持英语 ('en'),但我们显示的是马拉地语 ('mr') 语言的文本。

  pdfDownloadWithPuppeteer: async (req, res, err) => {
        const browser = await puppeteer.launch(
            {
                headless: true,
                executablePath: '/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome',
                args: \['--no-sandbox', '--disable-setuid-sandbox'\]

            });
        const page = await browser.newPage();
        // added below code for setting up language headers
        await page.setExtraHTTPHeaders({
            'Accept-Language': 'mr'
        });
        await page.goto( myWebUrl, { waitUntil: 'networkidle0' }), { waitUntil: 'load', timeout: 0 });
            const pdf = await page.pdf({ format: 'A4', printBackground: true });
            await browser.close();
            fs.writeFile('myProfile.pdf', pdf, function (err) {
                debugger
                if (err) {
                    console.log("server error");
                }
                res.download('myProfile.pdf')
            })
  • 添加了屏幕截图(仅呈现英语)

node.js puppeteer headless-browser
3个回答
1
投票

发生这种情况是因为您的服务器运行的系统中不存在该字体,因此请在您的服务器系统中安装所需的字体。 对于 Ubuntu 系统,您可以参考此链接 https://help.ubuntu.com/community/Fonts


0
投票

我为ubuntu安装了阿拉伯字体 然后就可以了

sudo apt-get update -y

sudo apt-get install -y fonts-kacst-one


0
投票

我也面临着同样的问题。我无法在 puppeteer 生成的 PDF 中显示其他语言字体。

请按照以下步骤操作:

  1. 从这里下载字体:https://help.ubuntu.com/community/Fonts
  2. 解压并将字体复制到
    /usr/local/share/fonts/
  3. 然后运行此命令
    sudo fc-cache -fv

而且,现在这对我有用。我希望它也对你有用。

© www.soinside.com 2019 - 2024. All rights reserved.