0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

svg2pdf

Posted at

input.svgoutput.pdf を生成します。

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    // SVGファイルを読み込み
    const svgContent = fs.readFileSync('input.svg', 'utf8');

    // viewBoxを取得してサイズを設定
    const [, , width, height] = svgContent.match(/viewBox="([-.\d\s]+)"/)[1].split(' ').map(Number);

    // HTMLにSVGを埋め込み、余白を削除
    await page.setContent(`<html><body style="margin: 0;">${svgContent}</body></html>`);
    await page.setViewport({ width: Math.ceil(width), height: Math.ceil(height) });

    // PDFを生成
    await page.pdf({
        path: 'output.pdf',
        width: `${width}px`,
        height: `${height}px`,
        margin: { top: 0, right: 0, bottom: 0, left: 0 },
        printBackground: true,
    });

    await browser.close();
    console.log('余白なしPDFを生成したのじゃ!');
})();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?