Retriving a pdf from nodejs and displaying it in a laravel response

Viewed 31

Good day everyone,

I'm trying to make a request to one of my Node.js servers to generate a PDF and then retrieve that PDF and display it in the response. I am currently getting a "Failed to load PDF document." error. Can someone just point me to the right direction? or help. Much appreciated.

Node.js

app.post("/", function (req, res) {
  console.log(req.body.name);
  const canvas = createCanvas(1190, 1200, "pdf");
  const ctx = canvas.getContext("2d");
  const canvasWidth = 1190;

  loadImage("./assets/pamphlet.png").then((image) => {
    ctx.drawImage(image, 0, 0);
    ctx.font = "63px Montserrat";
    ctx.fillStyle = "black";
    ctx.textAlign = "center";

    ctx.fillText(`${req.body.name.toUpperCase()}`, canvasWidth / 2, 95);

    let buffer = canvas.toBuffer("application/pdf", {
      title: `${req.body.name}-pamphlet`,
      creationDate: new Date(),
    });
    res.setHeader("Content-type", "application/pdf");
    res.send(buffer);
  });
});

Laravel

Route::get('/test', function () {
    $headers = [
        'Content-type' => 'application/pdf',
    ];

    $callback = function () {
        Http::asForm()->post('http://localhost:3000', [
            'name' => 'test',
        ]);
    };

    return Response::stream($callback, 200, $headers)->sendContent();
});
0 Answers
Related