https://drive.google.com/viewer?url=https://www.labnol.org/files/word.docx
使用插件將html -> pdf
要在React中使用react-pdf將一段HTML代碼轉換為PDF,您可以按照以下步驟進行操作:
1. 安裝react-pdf:在您的React項目中,使用npm或yarn安裝react-pdf庫。
? ?如果使用npm:
? ?```
? ?npm install @react-pdf/renderer
? ?```
? ?或者如果使用yarn:
? ?```
? ?yarn add @react-pdf/renderer
? ?```
2. 創建一個React組件:創建一個React組件,用于渲染您要轉換為PDF的HTML代碼。
? ?```javascript
? ?import React from 'react';
? ?import { Page, Document, StyleSheet } from '@react-pdf/renderer';
? ?const MyPDF = () => {
? ? ?return (
? ? ? ?<Document>
? ? ? ? ?<Page>
? ? ? ? ? ?{/* 在這里放置您要轉換為PDF的HTML代碼 */}
? ? ? ? ? ?<div>
? ? ? ? ? ? ?<h1>Hello, PDF!</h1>
? ? ? ? ? ? ?<p>This is an example HTML content.</p>
? ? ? ? ? ?</div>
? ? ? ? ?</Page>
? ? ? ?</Document>
? ? ?);
? ?};
? ?export default MyPDF;
? ?```
? ?在上述示例中,我們創建了一個名為`MyPDF`的React組件。在`<Document>`和`<Page>`組件中,我們可以放置要轉換為PDF的HTML代碼。在這個例子中,我們簡單地放置了一個包含標題和段落的`<div>`。
3. 使用react-pdf渲染PDF:在您的React應用中,您可以使用react-pdf的`<PDFViewer>`組件或`<PDFDownloadLink>`組件來渲染和下載PDF。
? ?a. 使用`<PDFViewer>`組件:如果您想在應用中直接顯示PDF,您可以使用`<PDFViewer>`組件。
? ?```javascript
? ?import React from 'react';
? ?import { PDFViewer } from '@react-pdf/renderer';
? ?import MyPDF from './MyPDF';
? ?const App = () => {
? ? ?return (
? ? ? ?<div>
? ? ? ? ?{/* 在這里放置其他內容 */}
? ? ? ? ?<PDFViewer>
? ? ? ? ? ?<MyPDF />
? ? ? ? ?</PDFViewer>
? ? ? ?</div>
? ? ?);
? ?};
? ?export default App;
? ?```
? ?在上述示例中,我們將`<MyPDF>`組件包裹在`<PDFViewer>`組件中,這樣就可以在應用中顯示PDF。
? ?b. 使用`<PDFDownloadLink>`組件:如果您想提供一個下載鏈接,讓用戶點擊下載PDF,您可以使用`<PDFDownloadLink>`組件。
? ?```javascript
? ?import React from 'react';
? ?import { PDFDownloadLink } from '@react-pdf/renderer';
? ?import MyPDF from './MyPDF';
? ?const App = () => {
? ? ?return (
? ? ? ?<div>
? ? ? ? ?{/* 在這里放置其他內容 */}
? ? ? ? ?<PDFDownloadLink document={<MyPDF />} fileName="mypdf.pdf">
? ? ? ? ? ?{({ blob, url, loading, error }) =>
? ? ? ? ? ? ?loading ? 'Loading document...' : 'Download now!'
? ? ? ? ? ?}
? ? ? ? ?</PDFDownloadLink>
? ? ? ?</div>
? ? ?);
? ?};
? ?export default App;
? ?```
? ?在上述示例中,我們將`<MyPDF>`組件作為`<PDFDownloadLink>`組件的`document`屬性傳遞,并指定要下載的PDF文件名為"mypdf.pdf"。根據`blob`、`url`、`loading`和`error`屬性,我們可以在組件中根據下載狀態顯示相應的內容。
請注意,以上示例是基本的示例,您可以根據自己的需求和具體的HTML代碼進行調整和擴展。您可以在`<MyPDF>`組件中放置更復雜的HTML代碼,并使用react-pdf提供的其他組件和樣式進行更高級的PDF生成和自定義。
方法 1?
要在React中使用Mammoth將Blob類型轉換為HTML,您可以按照以下步驟進行操作:
1. 首先,確保您的React項目已經安裝了Mammoth。您可以使用以下命令來安裝它:
```shell
npm install mammoth
```
2. 在您的React組件文件中,導入Mammoth庫:
```javascript
import * as mammoth from "mammoth";
```
3. 創建一個處理Blob轉換的函數。這個函數將接受一個Blob對象作為參數,并返回一個Promise,以便在轉換完成后進行處理。可以在組件類或函數組件中創建此函數。
```javascript
function convertBlobToHTML(blob) {
? return new Promise((resolve, reject) => {
? ? const reader = new FileReader();
? ? reader.onloadend = function () {
? ? ? const arrayBuffer = reader.result;
? ? ? mammoth.extractRawText({ arrayBuffer: arrayBuffer })
? ? ? ? .then((result) => {
? ? ? ? ? const html = result.value; // 轉換后的HTML內容
? ? ? ? ? resolve(html);
? ? ? ? })
? ? ? ? .catch((error) => {
? ? ? ? ? reject(error);
? ? ? ? });
? ? };
? ? reader.readAsArrayBuffer(blob);
? });
}
```
4. 在您的React組件中,使用上述函數來處理Blob對象:
```javascript
function MyComponent() {
? // 處理Blob轉換的函數
? const handleConvertBlob = async (blob) => {
? ? try {
? ? ? const html = await convertBlobToHTML(blob);
? ? ? // 在這里可以處理轉換后的HTML內容
? ? ? console.log(html);
? ? } catch (error) {
? ? ? console.error("轉換出錯:", error);
? ? }
? };
? // 示例調用
? const exampleBlob = new Blob([/* Blob數據 */], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
? handleConvertBlob(exampleBlob);
? return (
? ? // 組件的JSX內容
? );
}
```
請注意,上述代碼僅提供了將Blob轉換為HTML的基本邏輯,您可能需要根據具體的需求進行適當的調整和錯誤處理。同時,您需要確保正確地引入Mammoth庫并在項目中包含所需的文件。
方法2 codebox上的
https://codesandbox.io/p/devbox/brave-vaughan-gx8rk?file=%2Fsrc%2Fcomponents%2Fdrivers%2Fdocx-viewer.jsx%3A3%2C1-42%2C1
這段代碼是一個React組件,它使用了`mammoth`庫來將DOCX文件轉換為HTML,并在組件的`componentDidMount`生命周期方法中執行轉換操作。
代碼分析如下:
1. 導入React和mammoth庫:
```javascript
import React, { Component } from 'react';
import mammoth from 'mammoth';
```
2. 導入樣式和Loading組件:
```javascript
import 'styles/docx.scss';
import Loading from '../loading';
```
3. 定義一個繼承自`Component`的類組件:
```javascript
export default class extends Component {
? // 組件的生命周期方法
? componentDidMount() {
? ? // 創建一個XMLHttpRequest對象
? ? const jsonFile = new XMLHttpRequest();
? ? // 發送GET請求,獲取DOCX文件的路徑
? ? jsonFile.open('GET', this.props.filePath, true);
? ? jsonFile.send();
? ? jsonFile.responseType = 'arraybuffer';
? ? jsonFile.onreadystatechange = () => {
? ? ? // 檢查請求狀態和響應狀態碼
? ? ? if (jsonFile.readyState === 4 && jsonFile.status === 200) {
? ? ? ? // 使用mammoth庫將DOCX轉換為HTML
? ? ? ? mammoth.convertToHtml(
? ? ? ? ? { arrayBuffer: jsonFile.response },
? ? ? ? ? { includeDefaultStyleMap: true },
? ? ? ? )
? ? ? ? .then((result) => {
? ? ? ? ? // 創建一個div元素,并將轉換后的HTML內容賦值給innerHTML
? ? ? ? ? const docEl = document.createElement('div');
? ? ? ? ? docEl.className = 'document-container';
? ? ? ? ? docEl.innerHTML = result.value;
? ? ? ? ? // 將轉換后的HTML內容插入到id為'docx'的元素中
? ? ? ? ? document.getElementById('docx').innerHTML = docEl.outerHTML;
? ? ? ? })
? ? ? ? .catch((a) => {
? ? ? ? ? console.log('alexei: something went wrong', a);
? ? ? ? })
? ? ? ? .done();
? ? ? }
? ? };
? }
? // 渲染組件
? render() {
? ? return (
? ? ? <div id="docx">
? ? ? ? <Loading />
? ? ? </div>);
? }
}
```
此組件的渲染函數(`render`)返回一個`div`元素,其中包含一個id為'docx'的容器元素和一個`Loading`組件。在組件的`componentDidMount`方法中,使用XMLHttpRequest對象獲取指定路徑的DOCX文件,然后通過`mammoth`庫將其轉換為HTML。轉換完成后,將轉換后的HTML內容插入到id為'docx'的容器元素中。
請注意,該代碼中的文件路徑(`this.props.filePath`)和樣式文件路徑(`styles/docx.scss`)是需要根據實際情況進行調整的。此外,還需要確保項目中正確引入了`mammoth`庫和`Loading`組件的文件。
在react中展示HTML
在React組件中展示HTML內容可以使用`dangerouslySetInnerHTML`屬性。這個屬性允許您將HTML字符串作為內容插入到組件的DOM元素中,但需要注意潛在的安全風險。
以下是一個示例展示如何在React組件中展示HTML內容:
```javascript
import React from 'react';
function MyComponent() {
? const html = '<p>This is a paragraph.</p>';
? return (
? ? <div dangerouslySetInnerHTML={{ __html: html }} />
? );
}
export default MyComponent;
```
在上述示例中,我們定義了一個變量`html`,其中包含要展示的HTML內容。然后,我們在`<div>`元素上使用了`dangerouslySetInnerHTML`屬性,并將`html`變量設置為`__html`屬性的值。這樣React會將`html`中的內容作為HTML代碼插入到`<div>`元素中。
需要注意的是,`dangerouslySetInnerHTML`屬性的命名是有意義的,它提醒我們在使用時要小心,確保插入的HTML內容來自可信的來源,以防止跨站腳本攻擊(XSS)等安全問題。請確保您信任并驗證要展示的HTML內容,以防止潛在的安全風險。
此外,還可以考慮使用其他庫或組件來處理HTML內容的展示和渲染,例如`react-html-parser`或`react-render-html`等庫。這些庫提供更多的功能和選項,可以更安全地處理和渲染HTML內容。