{"id":19661966,"url":"https://github.com/akobashikawa/angular-docx2html","last_synced_at":"2026-05-11T13:38:54.789Z","repository":{"id":41877995,"uuid":"509607839","full_name":"akobashikawa/angular-docx2html","owner":"akobashikawa","description":"Cómo usar docx2html con angular","archived":false,"fork":false,"pushed_at":"2022-07-04T18:48:34.000Z","size":778,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-27T03:18:45.442Z","etag":null,"topics":["angular","docx2html"],"latest_commit_sha":null,"homepage":"https://angular-docx2html.netlify.app/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akobashikawa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-01T22:57:34.000Z","updated_at":"2022-07-04T18:47:24.000Z","dependencies_parsed_at":"2022-08-31T06:10:55.077Z","dependency_job_id":null,"html_url":"https://github.com/akobashikawa/angular-docx2html","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/akobashikawa/angular-docx2html","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akobashikawa%2Fangular-docx2html","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akobashikawa%2Fangular-docx2html/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akobashikawa%2Fangular-docx2html/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akobashikawa%2Fangular-docx2html/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akobashikawa","download_url":"https://codeload.github.com/akobashikawa/angular-docx2html/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akobashikawa%2Fangular-docx2html/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32897658,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-10T13:40:02.631Z","status":"online","status_checked_at":"2026-05-11T02:00:05.975Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","docx2html"],"created_at":"2024-11-11T16:09:09.491Z","updated_at":"2026-05-11T13:38:54.770Z","avatar_url":"https://github.com/akobashikawa.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Docx2html en Angular\n\ndocx2html es una biblioteca javascript que permite visualizar un documento doc.\n\nPero cómo usarlo en Angular?\n\nEste proyecto muestra una manera.\n\n[Demo](https://angular-docx2html.netlify.app/)\n## Instalación y despliegue\n\nEste es un proyecto generado con [Angular CLI](https://github.com/angular/angular-cli) version 14.0.2, así que puede proceder como de costumbre.\n\n```bash\n$ npm install\n$ ng serve\n```\n\n## docx2html\n\n[docx2html](https://github.com/lalalic/docx2html) es un convertidor javascript de docx a html. Ya sea en node como en el browser. Esta es una [demo](http://lalalic.github.io/docx2html/).\n\n`index.html`\n```html\n\u003cinput type=\"file\" onchange=\"onFileSelected(this)\" /\u003e\n\u003cdiv id=\"docx2html-container\"\u003e\u003c/div\u003e\n\u003cscript src=\"http://lalalic.github.io/docx2html/index.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  function onFileSelected(input){\n    require(\"docx2html\")(input.files[0],{container:document.querySelector(\"#docx2html-container\")})\n      .then(html=\u003e{\n        console.log(html.content);\n        // problema: mostrar todos los bookmarks del documento\n        // pista: estos son transformados en tags i \n        let ids = []\n        let everyChild = html.content.querySelectorAll(\"i\");\n        for (let i = 0; i \u003c everyChild.length; i++) {\n          console.log(everyChild[i].id);\n          ids.push(everyChild[i].id);\n        }\n        console.log(ids);\n      })\n  }\n\u003c/script\u003e\n```\n\n## Portando la solución a angular\n- Vemos que necesitaremos hacer uso de elementos del DOM:\n  - `#docx2html-container`\n  - `#docx2html-container i`\n- La biblioteca es un módulo pero no del tipo que puede ser importado directamente como módulo de angular.\n  - `$ npm install --save docx2html`\n  - `$ ls node_modules/docx2html/dist/docx2html.js`\n    - Versión para node\n  - `$ ls node_modules/docx2html/dist/index.js`\n    - Versión para el browser\n\n### Intento 1: KO\n`angular.json`\n```json\n  \"build:...\"\n    \"scripts\": [\n      \"node_modules/docx2html/dist/index.js\"\n    ]\n  \"test:...\"\n    \"scripts\": [\n      \"node_modules/docx2html/dist/index.js\"\n    ]\n```\n\n`src\\index.html`\n```html\n\u003capp-root\u003e\u003c/app-root\u003e\n\u003cscript\u003e\nconsole.log('docx2html', require('docx2html'));\n\u003c/script\u003e\n```\n\n- Parece como si el script `node_modules/docx2html/dist/index.js` no fuera cargado.\n  - Revisando en el inspector, veo que sí es cargado y combinado dentro de `scripts.js`\n  - Pero no funciona\n\n### Intento 2: OK\n- Deshago los cambios previos en `angular.json`\n\n`src\\index.html`\n```html\n\u003capp-root\u003e\u003c/app-root\u003e\n\u003cscript src=\"http://lalalic.github.io/docx2html/index.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\nconsole.log('docx2html', require('docx2html'));\n\u003c/script\u003e\n```\n\n- Cargo el script directamente del CDN\n\n### Técnica de carga diferida\n- Consiste en exponer `docx2html` a través del objeto `window` y luego cargarlo en el componente angular.\n\n`src/index.html`\n```html\n\u003capp-root\u003e\u003c/app-root\u003e\n\u003cdiv id=\"doc2html-container\"\u003e\u003c/div\u003e\n\u003cscript src=\"http://lalalic.github.io/docx2html/index.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  window.docx2html = require('docx2html');\n\u003c/script\u003e\n```\n\n`src/app/app.component.ts`\n```ts\ndeclare var docx2html: any;\n//...\nexport class AppComponent implements OnInit {\n  docx2html: any;\n\n  ngOnInit(): void {\n    this.docx2html = docx2html;\n    console.log('docx2html', docx2html);\n  }\n}\n\n```\n\n- Ahora ya tenemos a `doc2html` disponible en el componente, y `doc2html` cuenta con un elemento `#docx2html-container`, del DOM, que puede usar.\n\n## La solución\n`src/index.html`\n```html\n\u003capp-root\u003e\u003c/app-root\u003e\n\u003cdiv id=\"doc2html-container\"\u003e\u003c/div\u003e\n\u003cscript src=\"http://lalalic.github.io/docx2html/index.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  window.docx2html = require('docx2html');\n\u003c/script\u003e\n```\n\n`src/app/app.component.html`\n```html\n\u003ch1\u003edocx2html\u003c/h1\u003e\n\u003cinput type=\"file\" (change)=\"onFileSelected($event)\"\u003e\n\u003cul\u003e\n  \u003cli *ngFor=\"let id of ids\"\u003e\n    {{ id }}\n  \u003c/li\u003e\n\u003c/ul\u003e\n```\n\n`src/app/app.component.ts`\n```ts\ndeclare var docx2html: any;\n//...\nexport class AppComponent implements OnInit {\n  title = 'docx2html';\n  docx2html: any;\n  ids: any = [];\n\n  ngOnInit(): void {\n    this.docx2html = docx2html;\n    console.log('docx2html', docx2html);\n  }\n\n  onFileSelected(event: any) {\n    console.log('onFileSelected', event.target.files[0]);\n    this.docx2html(event.target.files[0], { container: document.querySelector(\"#doc2html-container\") })\n      .then((html:any) =\u003e {\n        console.log(html.content);\n        let ids = []\n        let everyChild = html.content.querySelectorAll(\"i\");\n        for (let i = 0; i \u003c everyChild.length; i++) {\n          console.log(everyChild[i].id);\n          ids.push(everyChild[i].id);\n        }\n        console.log(ids);\n        this.ids = ids;\n      })\n  }\n}\n\n```\n\n- Si no se quiere mostrar, `#doc2html-container` se puede ocultar con algo como:\n\n```html\n\u003cdiv id=\"doc2html-container\" style=\"width:0;height:0;\"\u003e\u003c/div\u003e\n```\n\n- Si se quiere una copia local, `http://lalalic.github.io/docx2html/index.js`, `node_modules/docx2html/dist/index.js`, se puede copiar como `assets/docx2html.browser.js`\n\n```html\n\u003cscript src=\"assets/docx2html.browser.js\"\u003e\u003c/script\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakobashikawa%2Fangular-docx2html","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakobashikawa%2Fangular-docx2html","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakobashikawa%2Fangular-docx2html/lists"}