Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosted Images with react-pdf

Here is a react-pdf example, and https://storage.googleapis.com/cbb_logos/conf_logos/conf-1-logo.png is a link to an image that is publicly hosted on GCS. Per GCS, "Anyone with this link can access the object on the public internet"

However, if i copy the link into line 11 in the react-pdf example, the image does not display. Per the docs for their image component, it clearly says: A React component for displaying network or local (Node only) JPG or PNG images, as well as base64 encoded image strings.

Simply put, am I out of luck on this, or is there another way to get hosted images in react-pdf? Perhaps another JS library is better for this? I don't think it's a good idea to save this image locally to my app, as I have 1000 more logos and that would increase my apps size by quite a bit.

Edit1: I gave a quick try using jsPDF using this stackblitz (not mine) https://stackblitz.com/edit/react-w5qb9a?file=src%2FApp.js

I dropped this div

<div style={{ maxWidth: '40px' }}>
    <img
        style={{width: '100%' }}
        src='https://storage.googleapis.com/cbb_logos/conf_logos/conf-1-logo.png' 
    />
</div>

...under the p-tag, then downloaded the PDF, and the image also did not appear. So I am still looking for a solution.

like image 894
Canovice Avatar asked Oct 20 '25 08:10

Canovice


1 Answers

I confirmed this works in a project as the docs say, but it doesn't work for some reason on their own example site, as you noted.

To test, I created a new react project using create-react-app:

yarn create react-app react-pdf-image

Install react-pdf:

cd react-pdf-image
yarn add @react-pdf/renderer

Note: A new version of create-react-app was just released (5.0.0) which uses webpack v5, and at the moment some packages like react-pdf no longer work out of the box. I had to add additional configuration to webpack via the craco package.

Install additional packages:

yarn add process browserify-zlib stream-browserify util buffer assert @craco/craco

Change the scripts section in package.json as below:

  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },

Next create a new file in the root of the project craco.config.js with these contents:

const webpack = require("webpack");

module.exports = {
  webpack: {
    configure: {
      resolve: {
        fallback: {
          process: require.resolve("process/browser"),
          zlib: require.resolve("browserify-zlib"),
          stream: require.resolve("stream-browserify"),
          util: require.resolve("util"),
          buffer: require.resolve("buffer"),
          asset: require.resolve("assert"),
        },
      },
      plugins: [
        new webpack.ProvidePlugin({
          Buffer: ["buffer", "Buffer"],
          process: "process/browser",
        }),
      ],
    },
  },
};

Hopefully this issue will be resolved and if so I'll remove these extra steps.

Finally, replace the contents of the src/App.js file with this code:

import { Document, Image, Page, PDFViewer, Text } from "@react-pdf/renderer";

const MyDocument = () => {
  return (
    <Document>
      <Page>
        <Text>Image test</Text>
        <Image src="https://storage.googleapis.com/cbb_logos/conf_logos/conf-1-logo.png" />
      </Page>
    </Document>
  );
};

const App = () => (
  <div>
    <PDFViewer>
      <MyDocument />
    </PDFViewer>
  </div>
);

export default App;

Back in the root of the project, run yarn start and you should see the rendered PDF with the text and image.

like image 155
liquidki Avatar answered Oct 22 '25 00:10

liquidki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!