Guest User

Untitled

a guest
Nov 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. /* eslint-disable no-unused-vars */
  2. import React from 'react';
  3. import warning from 'fbjs/lib/warning';
  4. import { flatStyles } from './utils/styles';
  5. import {
  6. pdf,
  7. View,
  8. Text,
  9. Link,
  10. Page,
  11. Font,
  12. Note,
  13. Image,
  14. version,
  15. StyleSheet,
  16. PDFRenderer,
  17. createInstance,
  18. Document as PDFDocument,
  19. } from './index';
  20.  
  21. export const Document = ({ children, ...props }) => {
  22. return <PDFDocument {...props}>{children}</PDFDocument>;
  23. };
  24.  
  25. class InternalBlobProvider extends React.PureComponent {
  26. state = { blob: null, url: null, loading: true, error: null };
  27.  
  28. constructor(props) {
  29. super(props);
  30.  
  31. // Create new root container for this render
  32. this.instance = pdf();
  33. }
  34.  
  35. componentDidMount() {
  36. this.renderDocument();
  37. this.onDocumentUpdate();
  38. }
  39.  
  40. componentDidUpdate() {
  41. this.renderDocument();
  42.  
  43. if (this.instance.isDirty()) {
  44. this.onDocumentUpdate();
  45. }
  46. }
  47.  
  48. renderDocument() {
  49. this.instance.updateContainer(this.props.document);
  50. }
  51.  
  52. onDocumentUpdate() {
  53. this.instance
  54. .toBlob()
  55. .then(blob => {
  56. this.setState({ blob, url: URL.createObjectURL(blob), loading: false });
  57. })
  58. .catch(error => {
  59. this.setState({ error });
  60. throw error;
  61. });
  62. }
  63.  
  64. render() {
  65. return this.props.children(this.state);
  66. }
  67. }
  68.  
  69. export const BlobProvider = ({ document: doc, children }) => {
  70. if (!doc) {
  71. warning(false, 'You should pass a valid document to BlobProvider');
  72. return null;
  73. }
  74.  
  75. return <InternalBlobProvider document={doc}>{children}</InternalBlobProvider>;
  76. };
  77.  
  78. export const PDFViewer = ({ className, style, children }) => {
  79. return (
  80. <InternalBlobProvider document={children}>
  81. {({ url }) => (
  82. <iframe
  83. className={className}
  84. src={url}
  85. style={Array.isArray(style) ? flatStyles(style) : style}
  86. />
  87. )}
  88. </InternalBlobProvider>
  89. );
  90. };
  91.  
  92. export const PDFDownloadLink = ({
  93. document: doc,
  94. className,
  95. style,
  96. children,
  97. fileName = 'document.pdf',
  98. }) => {
  99. if (!doc) {
  100. warning(false, 'You should pass a valid document to PDFDownloadLink');
  101. return null;
  102. }
  103.  
  104. const downloadOnIE = blob => () => {
  105. console.log("clicked");
  106. if (window.navigator.msSaveBlob) {
  107. window.navigator.msSaveBlob(blob, fileName);
  108. }
  109. };
  110.  
  111. return (
  112. <InternalBlobProvider document={doc}>
  113. {params => (
  114. <a
  115. className={className}
  116. download={fileName}
  117. href={params.url}
  118. onClick={downloadOnIE(params.blob)}
  119. style={Array.isArray(style) ? flatStyles(style) : style}
  120. >
  121. {typeof children === 'function' ? children(params) : children}
  122. </a>
  123. )}
  124. </InternalBlobProvider>
  125. );
  126. };
  127.  
  128. export {
  129. pdf,
  130. View,
  131. Text,
  132. Link,
  133. Page,
  134. Font,
  135. Note,
  136. Image,
  137. version,
  138. StyleSheet,
  139. PDFRenderer,
  140. createInstance,
  141. } from './index';
  142.  
  143. export default {
  144. pdf,
  145. View,
  146. Text,
  147. Link,
  148. Page,
  149. Font,
  150. Note,
  151. Image,
  152. version,
  153. Document,
  154. PDFViewer,
  155. StyleSheet,
  156. PDFRenderer,
  157. BlobProvider,
  158. createInstance,
  159. PDFDownloadLink,
  160. };
Add Comment
Please, Sign In to add comment