question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Text Problem when converting SVG to PNG on AWS Lambda

See original GitHub issue

What is the output of running npx envinfo --binaries --languages --system --utilities? My code is running on AWS Lambda Node 10, so I can not invoke this command.

What are the steps to reproduce? On AWS Lambda I run the following function :

...
exports.handler = async function(event) {
  const result = await sharp("/tmp/ImageTemplateBis.svg").png().toBuffer();
  return {
    statusCode: 200,
    headers: {
      'Content-Type': 'image/png' 
    },
    body: result.toString('base64'),
    isBase64Encoded: true
  }

It works. The following image is returned. But the text rendering fails (I get rectangles instead of letters) image

What is the expected behaviour? Running this code locally (on Mac) and storing it to a file results in the following png with pretty letters : image

I attached the svg in case this helps. ImageTemplateBis.svg.zip

Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
matt-gohrcommented, Apr 22, 2020

THANKS! Figured it out! Going to add my input here on how I got this to work incase anyone else is having trouble rendering fonts in AWS Lambda like this ticket describes. Also thanks to @lovell for being super responsive to all of the tickets and keeping up with the project.

Since the aws lambda environment doesn’t seem like it has fonts, you have to download them, package them up with your project and reference the path to them (so sharp can get to them) with the FONTCONFIG_PATH env set to where the fonts will be.

So need to create a fonts.conf file that has this XML in it, in the same folder where you store the fonts. I stored mine in a folder called fonts

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <dir>/var/task/fonts/</dir>
  <cachedir>/tmp/fonts-cache/</cachedir>
  <config></config>
</fontconfig>

Then in your function set the font path to this: process.env.FONTCONFIG_PATH='/var/task/fonts'

the var/task/ path is where your lambda function lives on the aws lambda container. So you just need to create a folder in you project called fonts and put this fonts.conf file in it along with a font file. And boom, you’ve got fonts with your shiney new PNG image.

Note: that the font file has to be a .ttf font file type like Roboto-Regular.ttf from google fonts.

3reactions
corybillcommented, Mar 5, 2020

@MattGohr First of all THANK YOU!!!

Second, its fonts.conf not font.conf. Can you please update your post. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS lambda convert svg to png - node.js - Stack Overflow
I would like to use the AWS Lambda service to convert SVG to PNG with the language NodeJS. In a second time, the...
Read more >
[Solved]-AWS lambda convert svg to png-node.js
Coding example for the question AWS lambda convert svg to png-node.js. ... based on the styling that any bit of text is supposed...
Read more >
Convert SVG to PNG on Meteor - help
From my experience working with SVGs converting them to PNG, doesn't maintain quality of the image, you will probably end up with fuzzy...
Read more >
Content type conversions in API Gateway - AWS Documentation
When converting a text payload to a binary blob, API Gateway assumes that the text data is a base64-encoded string and outputs the...
Read more >
convert-svg-to-png - npm
Converts SVG to PNG using headless Chromium. Latest version: 0.6.4, last published: 7 months ago. Start using convert-svg-to-png in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found