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.

Image rendering on Node backend does not work

See original GitHub issue

Describe the bug I am generating the pdf on the backend using the example from the website await renderToFile(<MyDocument />, ${__dirname}/my-doc.pdf);

but i am unable to use the Image component. I have tried to use external images and also internal images but i see an empty space. I have tried the same code on the frontend with React and the Pdf is generated correctly, i have tried even with the PdfDownloadLink and it correctly renders the image.

To Reproduce

  1. generate an express app with nodejs
  2. generate a route where you return or generate the Pdf
const router = express.Router();

router.post(
  "/api/printTest",
  asyncHandler(async (req: Request, res: Response) => {
    try {
      const { source } = req.body as { source: any};

      const string = await renderToString(<DocumentWithImage source={source} />);
      // or       await renderToFile(<DocumentWithImage source={source} />, `${__dirname}/my-doc.pdf`);

      res.send(string);
    } catch (error) {
    
      res.status(400).send(new Error("PDF Error Generation"));
    }
  })
);

export { router as printRouter};

Expected behavior I should see the image inside the Pdf

Desktop (please complete the following information):

  • OS: [ Windows 10]
  • Browser [ chrome,]
  • React-pdf version [ v2.0.4]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
gabrielvincentcommented, Apr 13, 2021

I was able to achieve what you want by rendering using the renderToStream function:

// print.tsx

import { renderToStream } from '@react-pdf/renderer'
import express, { Request, Response } from 'express'
import asyncHandler from 'express-async-handler'
import React from 'react'
import { ImageDocument } from '../documents/ImageDocument'

import type { DocumentNode } from '@react-pdf/types'

const router = express.Router()

async function convertDocumentToBuffer(
  document: DocumentNode
): Promise<Buffer> {
  const stream = await renderToStream(document)
  return new Promise((resolve, reject) => {
    let buffers: Uint8Array[] = []
    stream.on('data', data => {
      buffers.push(data)
    })
    stream.on('end', () => {
      resolve(Buffer.concat(buffers))
    })
    stream.on('error', reject)
  })
}

router.post(
  '/api/print',
  asyncHandler(async (req: Request, res: Response) => {
    try {
      const document = await convertDocumentToBuffer(<ImageDocument />)
      res.send(document)
    } catch (error) {
      res.status(400).send(new Error('PDF Error Generation'))
    }
  })
)

export { router as printRouter }
1reaction
ravviscommented, Oct 24, 2021

@diegomura I’m able to reproduce the same issue in the latest version 2.0.20 as well. The renderToString does not render the images at all.

Here is a screenshot of the output PDF: Screenshot 2021-10-24 at 1 02 22 PM

Read more comments on GitHub >

github_iconTop Results From Across the Web

React: Image uploaded from node js backend is not rendering
The Account page makes a GET request to the backend retrieves the path however I can't get the image to display. The path...
Read more >
Images in my /public folder not rendering correctly
I deployed my node.js backend on render.com and it has a “/public” folder with all the images that are supposed to be called...
Read more >
How to fetch images from Node.js server ? - GeeksforGeeks
The image to be accessed (geeksforgeeks.png) is placed inside the images folder, ... Run the server.js file using the following command:
Read more >
Why the Hell Would I Use Node.js? A Case-by-case Tutorial
Node.js can solve I/O scaling, but was not created to compute scaling problems. Learn why and when to use Node.js in this case-by-case...
Read more >
Backend rendering of a preview image - three.js forum
I have a working threejs-based viewer an now i like to generate ... to a image by click on a button on the...
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