Skip to content

Commit

Permalink
fix: images not right
Browse files Browse the repository at this point in the history
  • Loading branch information
BUPTlhuanyu committed Apr 16, 2022
1 parent 696dedf commit dccc831
Show file tree
Hide file tree
Showing 29 changed files with 124 additions and 21 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
*.log*
.vscode
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"command": "npm run debug",
"name": "debug",
"request": "launch",
"type": "node-terminal",
"cwd": "${workspaceFolder}/test"
},
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/index.js"
}
]
}
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 3.0.2
- [fix] zipReady event handler do not have zip instance
- [fix] image not right

# 3.0.1
- [fix] dir not found, writefile error

# 3.0.0
- [feature] support images
- [feature] support font color and font size
- [add] support images
- [add] support font color and font size
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# word-to-html

TODO:
- use tapple to provide hooks for user to handle html string;
- images is not right;

## feature
- images
- font-size color/style
Expand Down Expand Up @@ -47,7 +43,10 @@ const word2html = new Word2html(
}
);
word2html('zipReady', (zip) => {
// https://github.com/cthackers/adm-zip/wiki/ADM-ZIP#introduction
zip.getEntries();
// extractAllTo
zip.extractAllTo(path.resolve(__dirname, 'temp/entryfiles'));
});
word2html.convert();
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "word-to-html",
"version": "3.0.1",
"version": "3.0.2",
"description": "convert docx files to html",
"main": "src/index.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Builder {

getXmlDocument(xmlString) {
const domParser = new DOMParser();
return domParser.parseFromString(xmlString, "application/xml");;
return domParser.parseFromString(xmlString, "application/xml");
}

/**
Expand All @@ -41,8 +41,8 @@ class Builder {
* @param {*} file
* @returns
*/
collectImgs(zip, file) {
return collectImgs(zip, file);
collectImgs(zip, file, DOMParser) {
return collectImgs(zip, file, DOMParser);
}

/**
Expand All @@ -65,13 +65,13 @@ class Builder {
}

this.zip = this.zipFile();
this.imagePaths = this.collectImgs(this.zip, this.outputfile);
this.ins.emit('zipReady', this.file);
this.imgMap = this.collectImgs(this.zip, this.outputfile, DOMParser);
this.ins.emit('zipReady', this.zip, this.file);

const xmlContent = this.getXmlStr(this.zip);
const xmlDocument = this.getXmlDocument(xmlContent);

const htmlContent = new Converter(xmlDocument, this.imagePaths).convert();
const htmlContent = new Converter(xmlDocument, this.imgMap).convert();
const htmlRes = this.template.tl + htmlContent + this.template.tr;

this.ins.emit('htmlReady', htmlRes);
Expand Down
19 changes: 16 additions & 3 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,26 @@ const rFn = function (rArray, context) {
* @return {string} textContent 返回图片对应的标签字符串
*/
const imgFn = function (r, context) {
if (!context || !Array.isArray(context.images)) {
// get rId
const blips = r.getElementsByTagName('a:blip') || [];
if (blips.length === 0) {
return '';
}

const rId = blips[0].getAttribute("r:embed");

if (!context || !context.images) {
return '';
}

const imgData = context.images.get(rId);

if (!imgData) {
return '';
}
// const descr = r.getElementsByTagName("pic:cNvPr")[0].getAttribute("name");
const src = context.images.pop();
// console.log("descr", descr);
let tText = `<br /><img src="${src}" style="width: 100%"/>`;
let tText = `<br /><img src="${imgData.path}" style="width: 100%"/>`;
return tText;
};

Expand Down
37 changes: 33 additions & 4 deletions src/plugins/collectImgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,51 @@ const path = require('path');
const fs = require('fs-extra');
const getAbspath = require('../lib/abspath');

module.exports = function (zip, targetFile) {
module.exports = function (zip, targetFile, DOMParser) {
const entries = zip.getEntries();
const imagePaths = [];
const imgMap = new Map();
const imgIdMap = new Map();
try {
const bufferData = zip.readFile('word/_rels/document.xml.rels');
const relationShipStr = bufferData.toString('utf8');
console.log(relationShipStr);
const domParser = new DOMParser();
const relationShipDom = domParser.parseFromString(relationShipStr, "application/xml");
const relationships = relationShipDom.getElementsByTagName('Relationship');
for (let rels of relationships) {
const Target = `word/${rels.getAttribute('Target')}`;
const Id = rels.getAttribute('Id');
imgIdMap.set(Target, Id);
}
} catch (err) {
console.warn('Image map error');
}

for (let entry of entries) {
const PICTURE_EXPRESSION = /\.(png|jpe?g|gif|svg)(\?.*)?$/;
const picReg = new RegExp(PICTURE_EXPRESSION);
if (picReg.test(entry.name)) {
let name = entry.name;
const entryName = entry.entryName;
const imgPath = path.dirname(targetFile) + path.sep + entry.name;
fs.outputFileSync(
imgPath,
entry.getData(),
);
imagePaths.push(imgPath);

const id = imgIdMap.get(entryName);
imgMap.set(id, {
path: imgPath,
name,
entryName,
id
});

if (!id) {
console.log(`${entryName} has no id,it is must be some error happened in adm-zip`);
}
console.log(`${name} is generated`);
}
}
return imagePaths;
return imgMap;
}
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ const word2html = new Word2html(
outputDir: path.resolve(__dirname, 'temp')
}
);
word2html.on('zipReady', zip => {
zip.extractAllTo(path.resolve(__dirname, 'temp/entryfiles'));
});
word2html.convert();
2 changes: 2 additions & 0 deletions test/temp/entryfiles/[Content_Types].xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="png" ContentType="image/png"/><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Default Extension="dat" ContentType="text/plain"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/customXml/itemProps1.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml"/><Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/><Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/><Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/><Override PartName="/word/webSettings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"/><Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/><Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/><Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/><Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/><Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/></Types>
2 changes: 2 additions & 0 deletions test/temp/entryfiles/_rels/.rels
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="Rc32cd34c82b04ce5" Type="http://schemas.microsoft.com/office/2006/relationships/txt" Target="udata/data.dat"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/></Relationships>
2 changes: 2 additions & 0 deletions test/temp/entryfiles/customXml/_rels/item1.xml.rels
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps" Target="itemProps1.xml"/></Relationships>
1 change: 1 addition & 0 deletions test/temp/entryfiles/customXml/item1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><s:customData xmlns="http://www.wps.cn/officeDocument/2013/wpsCustomData" xmlns:s="http://www.wps.cn/officeDocument/2013/wpsCustomData"><customSectProps><customSectPr/></customSectProps></s:customData>
2 changes: 2 additions & 0 deletions test/temp/entryfiles/customXml/itemProps1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ds:datastoreItem ds:itemID="{B1977F7D-205B-4081-913C-38D41E755F92}" xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml"><ds:schemaRefs><ds:schemaRef ds:uri="http://www.wps.cn/officeDocument/2013/wpsCustomData"/></ds:schemaRefs></ds:datastoreItem>
2 changes: 2 additions & 0 deletions test/temp/entryfiles/docProps/app.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal</Template><TotalTime>148</TotalTime><Pages>1</Pages><Words>26</Words><Characters>153</Characters><Application>Microsoft Office Word</Application><DocSecurity>0</DocSecurity><Lines>1</Lines><Paragraphs>1</Paragraphs><ScaleCrop>false</ScaleCrop><Company>Allianz</Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>178</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>16.0000</AppVersion></Properties>
2 changes: 2 additions & 0 deletions test/temp/entryfiles/docProps/core.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:creator>Jenny Lin</dc:creator><cp:lastModifiedBy>廖焕宇</cp:lastModifiedBy><cp:revision>33</cp:revision><dcterms:created xsi:type="dcterms:W3CDTF">2019-07-16T19:45:00Z</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">2019-10-28T04:41:00Z</dcterms:modified></cp:coreProperties>
2 changes: 2 additions & 0 deletions test/temp/entryfiles/docProps/custom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="KSOProductBuildVer"><vt:lpwstr>2052-1.6.1.2429</vt:lpwstr></property></Properties>
1 change: 1 addition & 0 deletions test/temp/entryfiles/udata/data.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g��>�X��x��e�%L��ͪ��\��'%���N�̼hCUL+���-&�NOv޲�����"ٰ��n陭�]4�J�p���\ ����?������ =O�姺E�;t�ii�b���o���'P
2 changes: 2 additions & 0 deletions test/temp/entryfiles/word/_rels/document.xml.rels
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image3.png"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image2.png"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item1.xml"/><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/><Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/><Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/></Relationships>
2 changes: 2 additions & 0 deletions test/temp/entryfiles/word/document.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions test/temp/entryfiles/word/fontTable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:fonts xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" mc:Ignorable="w14 w15 w16se"><w:font w:name="Times New Roman"><w:panose1 w:val="02020603050405020304"/><w:charset w:val="00"/><w:family w:val="roman"/><w:pitch w:val="variable"/><w:sig w:usb0="E0002EFF" w:usb1="C000785B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/></w:font><w:font w:name="Calibri"><w:altName w:val="Arial Rounded MT Bold"/><w:panose1 w:val="020F0502020204030204"/><w:charset w:val="00"/><w:family w:val="swiss"/><w:pitch w:val="variable"/><w:sig w:usb0="E0002AFF" w:usb1="C000247B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/></w:font><w:font w:name="宋体"><w:altName w:val="SimSun"/><w:panose1 w:val="02010600030101010101"/><w:charset w:val="86"/><w:family w:val="auto"/><w:pitch w:val="variable"/><w:sig w:usb0="00000003" w:usb1="288F0000" w:usb2="00000016" w:usb3="00000000" w:csb0="00040001" w:csb1="00000000"/></w:font><w:font w:name="Arial"><w:panose1 w:val="020B0604020202020204"/><w:charset w:val="00"/><w:family w:val="swiss"/><w:pitch w:val="variable"/><w:sig w:usb0="E0002EFF" w:usb1="C000785B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/></w:font><w:font w:name="黑体"><w:altName w:val="SimHei"/><w:panose1 w:val="02010609060101010101"/><w:charset w:val="86"/><w:family w:val="modern"/><w:pitch w:val="fixed"/><w:sig w:usb0="800002BF" w:usb1="38CF7CFA" w:usb2="00000016" w:usb3="00000000" w:csb0="00040001" w:csb1="00000000"/></w:font><w:font w:name="仿宋_GB2312"><w:altName w:val="仿宋"/><w:charset w:val="86"/><w:family w:val="modern"/><w:pitch w:val="default"/><w:sig w:usb0="00000000" w:usb1="00000000" w:usb2="00000010" w:usb3="00000000" w:csb0="00040000" w:csb1="00000000"/></w:font><w:font w:name="仿宋体"><w:altName w:val="苹方-简"/><w:charset w:val="86"/><w:family w:val="roman"/><w:pitch w:val="default"/><w:sig w:usb0="00000000" w:usb1="00000000" w:usb2="00000010" w:usb3="00000000" w:csb0="00040000" w:csb1="00000000"/></w:font><w:font w:name="PMingLiU"><w:altName w:val="新細明體"/><w:panose1 w:val="02010601000101010101"/><w:charset w:val="88"/><w:family w:val="auto"/><w:pitch w:val="default"/><w:sig w:usb0="00000000" w:usb1="00000000" w:usb2="00000010" w:usb3="00000000" w:csb0="00100000" w:csb1="00000000"/></w:font><w:font w:name="Arial Unicode MS"><w:panose1 w:val="020B0604020202020204"/><w:charset w:val="86"/><w:family w:val="auto"/><w:pitch w:val="default"/><w:sig w:usb0="FFFFFFFF" w:usb1="E9FFFFFF" w:usb2="0000003F" w:usb3="00000000" w:csb0="603F01FF" w:csb1="FFFF0000"/></w:font><w:font w:name="Cambria"><w:altName w:val="苹方-简"/><w:panose1 w:val="02040503050406030204"/><w:charset w:val="00"/><w:family w:val="roman"/><w:pitch w:val="variable"/><w:sig w:usb0="E00006FF" w:usb1="420024FF" w:usb2="02000000" w:usb3="00000000" w:csb0="0000019F" w:csb1="00000000"/></w:font><w:font w:name="Futura Bk"><w:altName w:val="Arial"/><w:charset w:val="00"/><w:family w:val="swiss"/><w:pitch w:val="default"/><w:sig w:usb0="00000000" w:usb1="00000000" w:usb2="00000000" w:usb3="00000000" w:csb0="0000009F" w:csb1="00000000"/></w:font><w:font w:name="楷体"><w:altName w:val="汉仪楷体KW"/><w:panose1 w:val="02010609060101010101"/><w:charset w:val="86"/><w:family w:val="modern"/><w:pitch w:val="fixed"/><w:sig w:usb0="800002BF" w:usb1="38CF7CFA" w:usb2="00000016" w:usb3="00000000" w:csb0="00040001" w:csb1="00000000"/></w:font><w:font w:name="DFKai-SB"><w:altName w:val="Arial Unicode MS"/><w:charset w:val="88"/><w:family w:val="script"/><w:pitch w:val="default"/><w:sig w:usb0="00000000" w:usb1="00000000" w:usb2="00000016" w:usb3="00000000" w:csb0="00100001" w:csb1="00000000"/></w:font><w:font w:name="华文行楷"><w:panose1 w:val="02010800040101010101"/><w:charset w:val="86"/><w:family w:val="auto"/><w:pitch w:val="variable"/><w:sig w:usb0="00000001" w:usb1="080F0000" w:usb2="00000010" w:usb3="00000000" w:csb0="00040000" w:csb1="00000000"/></w:font><w:font w:name="华文彩云"><w:altName w:val="Arial Unicode MS"/><w:panose1 w:val="02010800040101010101"/><w:charset w:val="86"/><w:family w:val="auto"/><w:pitch w:val="variable"/><w:sig w:usb0="00000001" w:usb1="080F0000" w:usb2="00000010" w:usb3="00000000" w:csb0="00040000" w:csb1="00000000"/></w:font></w:fonts>
Binary file added test/temp/entryfiles/word/media/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/temp/entryfiles/word/media/image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/temp/entryfiles/word/media/image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dccc831

Please sign in to comment.