Can netlify functions return pdf?

I made a netlify function that create a pdf. The code is as below.

exports.handler = (event, context, callback) => {
  if (event.httpMethod !== "POST") {
    return { statusCode: 405, body: "Method Not Allowed" };
  const params = JSON.parse(event.body);
  const templateName = params.templateName || "";
  const datas = params.datas || [];
  const token = params.token || "";
  if (!checkToken(token)) {
    return { statusCode: 403, body: "Invalid token" };
  const arg = { ...templateData[templateName], datas, preview: true };
  const docDefinition = pdfUtil.createDocDefinition(arg);
  getBlob(docDefinition).then(blob => {
    callback(null, {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/pdf"
      statusCode: 200,
      body: blob.toString("base64"),
      isBase64Encoded: true

This function is working on localhost but not in production. I receive the following error message

0:00:00 AM: error decoding lambda response: json: cannot unmarshal object into Go struct field lambdaInvokeResponse.body of type string

So, I found this discussion.

But, I don’t know what’s wrong.

Any idea what’s going on here?


How are you testing this locally? is it using netlify-lambda or the netlify CLI? Also, where do you define getBlob()? If you manually generate a BASE64 encoded pdf and return that as the body, do you have the same error? Just want to make sure that we have a small repro case that highlights the issue and we’re not testing to make sure your function is properly returning a blob in the body.

hand-dot did u have any success with this?

I haven’t changed the code, It’s working right now.

Really? So it just started working on production randomly?