PDF viewer in VR space based on React 360 and PDF.js
Inspired by
- facebookarchive/react-360#392
- https://github.com/LiuC520/ReactVR/blob/master/examples/show_pdf_in_react360/RADEME.md
You can show your pdf with GitHub Pages by cloning this repository and replacing the docs/static_assets/demo.pdf
.
- Install this package
$ npm install -S react-360-pdf-viewer
- Import
PDF.js
at index.html
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.550/pdf.min.js"></script>
- Install
PdfModule
in clinet.js
import { ReactInstance } from 'react-360-web';
import PdfModule from 'react-360-pdf-viewer/PdfModule';
function init(bundle, parent, options = {}) {
const r360 = new ReactInstance(bundle, parent {
// Add custom options here
fullScreen: true,
nativeModules: [
ctx => new PdfModule(ctx),
],
...options,
});
}
- Open document and render a page
import { NativeModules } from 'react-360';
const { PdfModule } = NativeModules;
const url = 'some.pdf';
PdfModule.getDocument(url).then(pdf => {
console.log(pdf.numPages);
// loading
return PdfModule.renderPage(1, 'pdf_texture');
}).then(() => {
// done
});
- Show the texture in render method
import React from 'react';
import { PdfPlane } from 'react-360-pdf-viewer';
class YourComponent extends React.Component {
render() {
return (
<PdfPlane width={720} height={540} texture="pdf_texture" loading={false} />
);
}
}
-
PdfModule.getDocument(url) -> Promise({numPages})
- execute
pdfjsLib.getDocument(url)
and store the result
- execute
-
PdfModule.renderPage(pagenum, textureName) -> Promise()
- render page
pagenum
of stored document to a canvas and register it as a texture namedtextureName
- render page
A React 360 component to show a rendered texture
- Properties
- width
- height
- texture: textureName to show
- loading: if true, hide the texture and print the text
loading
Copyright (c) 2018 Keisuke Minami
MIT