隨著人們對數字化的需求提高,操作文檔的頻率越來越高,由此產生了一系列的文檔工具。今天,我們要介紹的是一款完全開源的文檔操作工具——jsword。通過jsword,我們可以完成多種多樣的文檔操作,並且有著良好的擴展性,能夠滿足各種不同類型的文檔操作需求。下面我們將從多個角度進行jsword的介紹。
一、jsword男裝——快速了解jsword
首先我們需要知道,jsword是一款用於操作文檔的工具庫,使用它可以進行多種多樣的文檔操作,比如文檔預覽、文檔轉換等等。下面就給大家介紹一下使用jsword的步驟。首先我們需要安裝jsword:
npm install jsword
安裝完成之後,我們可以簡單了解一下jsword的基本用法,比如獲取dom節點,用法如下:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM(`Hello world
`);
console.log(dom.window.document.querySelector("p").textContent); // "Hello world"
還有操作pdf的插件pdfkit,使用如下:
const PDFDocument = require('pdfkit');
const fs = require('fs');
// Create a document
const doc = new PDFDocument();
// Pipe its output somewhere, like to a file or HTTP response
// See below for browser usage
doc.pipe(fs.createWriteStream('output.pdf'));
// Embed a font, set the font size, and render some text
doc.font('fonts/PalatinoBold.ttf')
.fontSize(25)
.text('Some text with an embedded font!', 100, 100);
// Add an image, constrain it to a given size, and center it vertically and horizontally
doc.image('path/to/image.png', {
fit: [250, 300],
align: 'center',
valign: 'center'
});
// Add another page
doc.addPage()
.fontSize(25)
.text('Here is some vector graphics...', 100, 100);
// Draw a triangle
doc.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.fill("#FF3300");
// Apply some transforms and render an SVG path with the 'even-odd' fill rule
doc.scale(0.6)
.translate(470, -380)
.path('M 250,75 L 323,301 131,161 369,161 177,301 z')
.fill('red', 'even-odd')
.restore();
// Add some text with annotations
doc.addPage()
.fillColor("blue")
.text('Here is a link!', 100, 100)
.underline(100, 100, 160, 27, {color: "#0000FF"})
.link(100, 100, 160, 27, 'http://google.com/');
// Finalize PDF file
doc.end();
如此簡單的引入,我們就可以開始操作文檔了。
二、jsword文檔轉pdf——多種操作,一站式解決
除了簡單的dom操作外,jsword也提供了其他多樣的文檔操作,比如文檔轉換。以文檔轉pdf為例,我們需要用到pdfmake這個庫,安裝方法如下:
npm install pdfmake
安裝好之後,我們就可以開始使用pdfmake了。下面我們用一個簡短的例子來介紹:將文檔轉成pdf
const fs = require('fs');
const pdfMake = require('pdfmake');
const docDefinition = { content: 'This is test content!' };
const pdfDoc = pdfMake.createPdf(docDefinition);
pdfDoc.getBase64((data) => {
fs.writeFileSync('output.pdf', data, 'base64');
});
代碼解釋:首先我們需要定義我們需要轉換的文檔內容docDefinition,然後通過createPdf將其轉換為pdf格式。最後保存成文件。
三、jsword轉pdf插件——擴展jsword功能的利器
上面提到了pdfmake這個庫,其實pdfmake是一款非常強大、且易於使用的pdf生成工具。將其引入到jsword中,可以使得我們的jsword有更多的pdf操作能力。下面我們需要安裝一個jsword的pdf插件:
npm install jsword-pdf-plugin
安裝好之後,我們就可以使用這個插件擴展jsword的pdf操作能力了。下面我們用一個例子來嘗試使用:
import PDFMake from '../../src/shared/pdfmake';
...
const pdf = new PDFMake();
const docDefinition = { content: 'This is test content!' };
pdf.download(docDefinition, 'output.pdf');
這個例子中,我們先引進了pdfmake,然後創建一個pdf對象,並且使用了pdf對象的download方法將文檔下載到本地。
四、jsword文檔預覽——快速查看文檔內容
我們還可以通過jsword來快速的預覽文檔。下面我們需要引入viewerjs這個庫:
npm install viewerjs
安裝完成之後,我們就可以使用viewerjs來預覽文檔了。下面給出一個例子
const Viewer = require('viewerjs');
const img = document.getElementById('image_id');
const viewer = new Viewer(img);
代碼解釋:我們首先需要找到要預覽的文檔,然後使用viewerjs的構造函數即可。
五、jsword轉html——快速轉換文檔格式
除了轉pdf操作,我們還可以使用jsword來實現文檔的html格式轉換。我們可以使用officegen插件來完成這項操作:
npm install officegen
安裝完成之後,我們就可以使用officegen來進行文檔轉換了。下面給出一個例子:
const officegen = require('officegen');
const fs = require('fs');
// 建立文檔
let docx = officegen('docx');
// 添加文字
docx.createP().addText("這是一個測試內容");
const out = fs.createWriteStream('output.docx');
// 將操作寫入文件
docx.generate(out);
代碼解釋:我們首先創建了一個文檔對象docx,然後添加了一些文本內容,最後將其生成到指定的文件中。
六、jsword換氣記號——解決文檔修復問題
jsword除了文檔操作外,還有自身的一些特色功能。比如,jsword可以讀取換氣記號,來讓我們更好的定位文檔中的問題。
const jsword = require('jsword');
let wordFile = null;
// 獲取word文件
jsword.read(wordFile).then(function(docx){
// 解析換氣記號
const breaks = docx.getBreaks();
console.log(breaks);
});
代碼解釋:我們通過jsword.read讀取word文件,然後使用docx.getBreaks來獲取文檔中的換氣記號,便於我們更好的定位到文檔的具體問題。
總結
以上就是對於jsword的介紹。這是一款非常強大、免費、開源的文檔操作工具,使用jsword,我們可以實現文檔的快速轉換、文檔預覽、文檔編輯等等多種操作。而且,由於jsword開源,我們還可以在其中加入自己所需要的特色功能。相信通過jsword的介紹,大家對於這款工具已經有了較為深入的了解,希望大家能夠在今後的操作當中有所收穫。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/198173.html