wkhtmltopdf missing SVG paths (Rendering)

后端 未结 2 2164
面向向阳花
面向向阳花 2021-02-19 02:41

I\'m using a image inside the HMTL code like here below:

相关标签:
2条回答
  • 2021-02-19 03:18

    A workaround is to convert the .SVG files to .PNG files before converting to .PDF. The .SVG code can be extracted via AgilityPack or read the files as a string. Then use the Svg library to convert to .PNG as follows:

    var svg = SvgDocument.FromSvg<SvgDocument>(svgXmlAsString);
    var bitmap = svg.Draw();
    var codec = "image/png".ParseImageCodecInfo();
    // intermediaryStream required due to image.Save attempting read access
    using (var intermediaryStream = new MemoryStream())
    {
        bitmap.Save(intermediaryStream, codec);
        intermediaryStream.Position = 0;
        await intermediaryStream.CopyToAsync(responseStream);
        await intermediaryStream.FlushAsync();
        await responseStream.FlushAsync();
    }
    
    0 讨论(0)
  • 2021-02-19 03:30

    So not a exact solution but a far better alternative now is to use a chrome in headless mode

    $ chrome --headless --disable-gpu --print-to-pdf test.html
    [0427/011400.636954:WARNING:dns_config_service_posix.cc(174)] dns_config has unhandled options!
    [0427/011400.638406:ERROR:gpu_process_transport_factory.cc(1007)] Lost UI shared context.
    [0427/011400.801881:INFO:headless_shell.cc(586)] Written to file output.pdf. 
    

    Also if you want better control on the process, you would use NodeJS and puppeteer

    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
      await page.pdf({path: 'hn.pdf', format: 'A4'});
    
      await browser.close();
    })();
    
    0 讨论(0)
提交回复
热议问题