Bibisoltan Rustamowa
Bibisoltan Rustamowa

Reputation: 25

how make flipbook react-pdf with turn.js?

I first time build flipbook app. but here error. if possible help me for build this. Here Turn component I build turn.js and not problem this component. When I show pdf file i only see pdf file not flipbook here my code:

import React, { useState, useEffect } from "react";
import { Document, Page } from "react-pdf";
import Turn from "./Turn"; // Import your Turn component here

interface PDFViewerProps {
  url: string;
}

const PDFViewer: React.FC<PDFViewerProps> = ({ url }) => {
  const [numPages, setNumPages] = useState<number | null>(null);
  const [canvasRefs, setCanvasRefs] = useState<Array<React.RefObject<HTMLCanvasElement>>>([]);
  const isTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
  const options = {
    width: 800,
    height: 600,
    autoCenter: true,
    display: "double",
    acceleration: true,
    elevation: 50,
    gradients: !isTouch,
  };

  useEffect(() => {
    const newCanvasRefs = Array.from({ length: numPages || 0 }, () => React.createRef<HTMLCanvasElement>());
    setCanvasRefs(newCanvasRefs);
  }, [numPages]);

  const handleDocumentLoadSuccess = ({ numPages }: { numPages: number }) => {
    setNumPages(numPages);
  };

  return (
    <div className="w-full h-full flex items-center justify-center">
      <Document
        file={url}
        onLoadSuccess={handleDocumentLoadSuccess}
        onLoadError={(err) => console.log(err.message, err.name)}
      >
        {Array.from({ length: numPages || 0 }, (_, index) => (
          <Page key={`page_${index + 1}`} pageNumber={index + 1}>
            {(props) => (
              <div style={{ width: "100%", height: "100%" }}>
                <Turn options={options} style={{ width: "100%", height: "100%" }}>
                  <canvas
                    ref={canvasRefs[index]}
                    {...props.canvasProps}
                    style={{ width: "100%", height: "100%" }}
                  />
                </Turn>
              </div>
            )}
          </Page>
        ))}
      </Document>
    </div>
  );
};

Upvotes: 0

Views: 151

Answers (0)

Related Questions