【问题标题】:Using Sharp to turn svg to png corrupts text in node.js使用 Sharp 将 svg 转换为 png 会破坏 node.js 中的文本
【发布时间】:2021-12-01 10:23:20
【问题描述】:

我正在使用Sharp将svg文件转换为png格式以上传到松弛工作区。

我发现了同样的问题here


 const options = {
    d3Module: d3,
    selector: '#chart',
    container: '<div id="container"><div id="chart"></div></div>'
  }
  const d3n = new D3Node(options);
  const margin = {
    top: 10, right: 5, bottom: 30, left: 5
  }

  const width = 1000 - margin.left - margin.right;
  const height = 400 - margin.top - margin.bottom;
  const svgWidth = width + margin.left + margin.right;
  const svgHeight = height + margin.top + margin.bottom;

  const svg = d3n.createSVG(svgWidth, svgHeight);
  const tempData = [{ year: 2020, value: 100 }, { year: 2019, value: 200 }, { year: 2018, value: 30 }, { year: 2017, value: 50 }, { year: 2016, value: 80 }];

  const xScale = d3.scaleBand().range([0,width]).padding(0.5);
  const yScale = d3.scaleLinear().range([height,0]);
  
  let yMax = d3.max(tempData, (d) => {return d.value})
  yMax += yMax * 0.3;
  xScale.domain(tempData.map((d) => {return d.year} ))
  yScale.domain([0,yMax]);

  svg.append('rect')
    .attr('width','100%')
    .attr('height', '100%')
    .style('fill','rgb(28, 35, 51);');

  svg.append('text')
    .attr('transform','translate(150,0)')
    .attr('fill','#85ceff')
    .attr('font-size','24px')
    .attr('x',50)
    .attr('y',50)
    .text('Node and D3 Bar chart')
  
  svg.append('g').attr('transform',`translate(${100}, ${100})`);

  svg.append('g')
    .attr('transform', `translate(50, ${height})`)
    .call(d3.axisBottom(xScale))
    .append('text')
    .attr('y', height-380)
    .attr('x',width-500)
    .attr('text-anchor','end')
    .attr('stroke','black')
    .attr('fill','#85ceff')
    .attr('font-size','20px')
    .text('Year')
  
  svg.append('g')
    .attr('transform','translate(50,0)')
    .call(d3.axisLeft(yScale).tickFormat((d) => {
      return `$${d}`;
    }).ticks(5))
    .append('text')
    .attr('transform','rotate(-90)')
    .attr('y',150)
    .attr('x',-150)
    .attr('dy','-9.1em')
    .attr('text-anchor','end')
    .attr('stroke','black')
    .attr('font-size','20px')
    .attr('fill','#85ceff')
    .text('Cost')

  fs.writeFileSync('out.svg', d3n.svgString());
  sharp('out.svg')
    .png()
    .toFile('sharp.png')
    .then((info) => {
      console.log("Svg to Png conversion completed", info);
    })
    .catch((error) => {
      console.log(error)
    })

但是,当我将svg 处理为png 时,我的文字已损坏并且不再出现在照片中。我在这里做错了什么?

澄清一下,我在replit 上运行此服务器。当我在本地运行此代码时,png 没有损坏。 replit服务器可能是问题吗?

【问题讨论】:

  • 如 GitHub 问题中所述,可能是无法找到字体文件的问题。你检查过这个其他issue

标签: node.js svg d3.js sharp


【解决方案1】:

在浏览了几个关于这个 [Link 1, Link 2, Link 3] 的 Github 问题后,我做了更多的研究,发现我需要一个字体配置文件和字体下载,因为 Replit 服务器没有字体已安装。

File Structure


    - index.js
    - api
     - fetch.js
    - fonts
     - fonts.conf
     - SourceSansPro-Regular.ttf

fonts.conf


     <?xml version="1.0"?>
     <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
     <fontconfig>
     <dir>/home/runner/gittrack/fonts</dir>
     <config></config>
     </fontconfig>

添加上述更改后,我的 png 上现在可以使用字体了

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 2015-10-04
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 2021-04-28
    • 2021-09-13
    相关资源
    最近更新 更多