React + PDFViewer with pdfjs-dist SOLUTION

I hope I don’t break any rules but this is a solution that took me a lot of time to find and I wish it would help someone else.

If you are using React and want to display a PDF there are several ways, but one of the ones that I needed required me to use the pdfjs-dist package (for some cases you might encounter the need for @react-pdf-viewer, which has the same relevance to here as well).

So, to make sure these packages work for you please make sure you use these versions:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"@react-pdf-viewer/core": "^3.4.0",
"pdfjs-dist": "^2.11.338",
</code>
<code>"@react-pdf-viewer/core": "^3.4.0", "pdfjs-dist": "^2.11.338", </code>
"@react-pdf-viewer/core": "^3.4.0",
"pdfjs-dist": "^2.11.338",

and if you want the toolbar use this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"@react-pdf-viewer/toolbar": "^3.4.0",
</code>
<code>"@react-pdf-viewer/toolbar": "^3.4.0", </code>
"@react-pdf-viewer/toolbar": "^3.4.0",

(for fast installation use this: npm install @react-pdf-viewer/[email protected] @react-pdf-viewer/[email protected] [email protected])

After installing all of the versions, in some required area of the code make these imports:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Worker, Viewer } from "@react-pdf-viewer/core";
import "@react-pdf-viewer/core/lib/styles/index.css";
import { toolbarPlugin } from "@react-pdf-viewer/toolbar";
import "@react-pdf-viewer/toolbar/lib/styles/index.css";
import { GlobalWorkerOptions } from "pdfjs-dist";
GlobalWorkerOptions.workerSrc = `https://unpkg.com/[email protected]/build/pdf.worker.min.js`;
</code>
<code>import { Worker, Viewer } from "@react-pdf-viewer/core"; import "@react-pdf-viewer/core/lib/styles/index.css"; import { toolbarPlugin } from "@react-pdf-viewer/toolbar"; import "@react-pdf-viewer/toolbar/lib/styles/index.css"; import { GlobalWorkerOptions } from "pdfjs-dist"; GlobalWorkerOptions.workerSrc = `https://unpkg.com/[email protected]/build/pdf.worker.min.js`; </code>
import { Worker, Viewer } from "@react-pdf-viewer/core";
import "@react-pdf-viewer/core/lib/styles/index.css";
import { toolbarPlugin } from "@react-pdf-viewer/toolbar";
import "@react-pdf-viewer/toolbar/lib/styles/index.css";
import { GlobalWorkerOptions } from "pdfjs-dist";
GlobalWorkerOptions.workerSrc = `https://unpkg.com/[email protected]/build/pdf.worker.min.js`;

I know it not the best practice to put the initialization at the top like so, but to be honest, if it ain’t broken don’t fix it.

After that, in the return part of the RFC do this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><BlobProvider
document={
<Document>
<Page>
<Text>Something something</Text>
</Page>
</Document>
}>
{({ loading, url, blob }) => (
<div style={{ height: "100%" }}>
{blob && (
<Worker workerUrl={`https://unpkg.com/[email protected]/build/pdf.worker.min.js`}>
<Viewer fileUrl={URL.createObjectURL(blob)} />
</Worker>
)}
</div>
)}
</BlobProvider>
</code>
<code><BlobProvider document={ <Document> <Page> <Text>Something something</Text> </Page> </Document> }> {({ loading, url, blob }) => ( <div style={{ height: "100%" }}> {blob && ( <Worker workerUrl={`https://unpkg.com/[email protected]/build/pdf.worker.min.js`}> <Viewer fileUrl={URL.createObjectURL(blob)} /> </Worker> )} </div> )} </BlobProvider> </code>
<BlobProvider
  document={
    <Document>
      <Page>
        <Text>Something something</Text>
      </Page>
    </Document>
  }>
  {({ loading, url, blob }) => (
    <div style={{ height: "100%" }}>
      {blob && (
        <Worker workerUrl={`https://unpkg.com/[email protected]/build/pdf.worker.min.js`}>
          <Viewer fileUrl={URL.createObjectURL(blob)} />
        </Worker>
      )}
    </div>
  )}
</BlobProvider>

And by then you should have a working PDF viewer that actually works.

I really hope this helped anyone, if not, then this is my way stressing out how terrible of an experience to find out how to make this work this was (especially the versions part).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật