fix(core): update
This commit is contained in:
commit
8bde0f1d21
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
name: Default (not tags)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags-ignore:
|
||||||
|
- '**'
|
||||||
|
|
||||||
|
env:
|
||||||
|
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||||
|
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||||
|
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||||
|
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||||
|
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Install pnpm and npmci
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @shipzone/npmci
|
||||||
|
|
||||||
|
- name: Run npm prepare
|
||||||
|
run: npmci npm prepare
|
||||||
|
|
||||||
|
- name: Audit production dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --prod
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
- name: Audit development dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --dev
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
test:
|
||||||
|
if: ${{ always() }}
|
||||||
|
needs: security
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Test stable
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm test
|
||||||
|
|
||||||
|
- name: Test build
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm build
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
name: Default (tags)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
env:
|
||||||
|
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||||
|
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||||
|
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||||
|
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||||
|
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @shipzone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Audit production dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --prod
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
- name: Audit development dependencies
|
||||||
|
run: |
|
||||||
|
npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
npmci command pnpm audit --audit-level=high --dev
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
test:
|
||||||
|
if: ${{ always() }}
|
||||||
|
needs: security
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @shipzone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Test stable
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm test
|
||||||
|
|
||||||
|
- name: Test build
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
npmci npm build
|
||||||
|
|
||||||
|
release:
|
||||||
|
needs: test
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @shipzone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Release
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm publish
|
||||||
|
|
||||||
|
metadata:
|
||||||
|
needs: test
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: ${{ env.IMAGE }}
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
run: |
|
||||||
|
pnpm install -g pnpm
|
||||||
|
pnpm install -g @shipzone/npmci
|
||||||
|
npmci npm prepare
|
||||||
|
|
||||||
|
- name: Code quality
|
||||||
|
run: |
|
||||||
|
npmci command npm install -g typescript
|
||||||
|
npmci npm install
|
||||||
|
|
||||||
|
- name: Trigger
|
||||||
|
run: npmci trigger
|
||||||
|
|
||||||
|
- name: Build docs and upload artifacts
|
||||||
|
run: |
|
||||||
|
npmci node install stable
|
||||||
|
npmci npm install
|
||||||
|
pnpm install -g @git.zone/tsdoc
|
||||||
|
npmci command tsdoc
|
||||||
|
continue-on-error: true
|
20
.gitignore
vendored
Normal file
20
.gitignore
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
.nogit/
|
||||||
|
|
||||||
|
# artifacts
|
||||||
|
coverage/
|
||||||
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"command": "npm test",
|
||||||
|
"name": "Run npm test",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "node-terminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
31
npmextra.json
Normal file
31
npmextra.json
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "gitea.nevermind.cloud",
|
||||||
|
"gitscope": "fin.cx",
|
||||||
|
"gitrepo": "xinvoice",
|
||||||
|
"description": "A module for creating, manipulating, and embedding XML data within PDF files for xinvoice packages.",
|
||||||
|
"npmPackagename": "@fin.cx/xinvoice",
|
||||||
|
"license": "MIT",
|
||||||
|
"projectDomain": "fin.cx",
|
||||||
|
"keywords": [
|
||||||
|
"invoice processing",
|
||||||
|
"PDF manipulation",
|
||||||
|
"XML embedding",
|
||||||
|
"financial documents",
|
||||||
|
"xinvoice",
|
||||||
|
"document generation",
|
||||||
|
"PDF library",
|
||||||
|
"typescript"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"npmci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"tsdoc": {
|
||||||
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
|
}
|
||||||
|
}
|
61
package.json
Normal file
61
package.json
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"name": "@fin.cx/xinvoice",
|
||||||
|
"version": "1.0.3",
|
||||||
|
"private": false,
|
||||||
|
"description": "A module for creating, manipulating, and embedding XML data within PDF files for xinvoice packages.",
|
||||||
|
"main": "dist_ts/index.js",
|
||||||
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
|
"author": "Task Venture Capital GmbH",
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"test": "(tstest test/ --web)",
|
||||||
|
"build": "(tsbuild --web --allowimplicitany)",
|
||||||
|
"buildDocs": "(tsdoc)"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@git.zone/tsbuild": "^2.1.25",
|
||||||
|
"@git.zone/tsbundle": "^2.0.5",
|
||||||
|
"@git.zone/tsrun": "^1.2.46",
|
||||||
|
"@git.zone/tstest": "^1.0.44",
|
||||||
|
"@push.rocks/tapbundle": "^5.0.15",
|
||||||
|
"@types/node": "^20.8.7"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@push.rocks/smartfile": "^11.0.14",
|
||||||
|
"pdf-lib": "^1.17.1"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://gitea.nevermind.cloud/fin.cx/xinvoice.git"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://gitea.nevermind.cloud/fin.cx/xinvoice/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://gitea.nevermind.cloud/fin.cx/xinvoice#readme",
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
],
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
|
"keywords": [
|
||||||
|
"invoice processing",
|
||||||
|
"PDF manipulation",
|
||||||
|
"XML embedding",
|
||||||
|
"financial documents",
|
||||||
|
"xinvoice",
|
||||||
|
"document generation",
|
||||||
|
"PDF library",
|
||||||
|
"typescript"
|
||||||
|
]
|
||||||
|
}
|
4696
pnpm-lock.yaml
generated
Normal file
4696
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
0
readme.hints.md
Normal file
0
readme.hints.md
Normal file
109
readme.md
Normal file
109
readme.md
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
# @fin.cx/xinvoice
|
||||||
|
a module for working with xinvoice packages.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
To install `@fin.cx/xinvoice`, you'll need npm (Node Package Manager). Run this command in your terminal:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npm install @fin.cx/xinvoice
|
||||||
|
```
|
||||||
|
|
||||||
|
This command fetches the xinvoice package from the npm registry and installs it in your project.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
The `@fin.cx/xinvoice` module is designed to work with xinvoice packages, specifically aimed at managing invoices with ease. This document will guide you through using all the features offered by the module, ensuring you can effectively incorporate it into your projects.
|
||||||
|
|
||||||
|
### Getting Started with ESM and TypeScript
|
||||||
|
|
||||||
|
To use `@fin.cx/xinvoice` in a TypeScript project, first, make sure you have configured TypeScript to support ECMAScript modules (ESM). Here's a basic `tsconfig.json` for reference:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "ESNext",
|
||||||
|
"target": "ESNext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"outDir": "./dist",
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
"include": ["src/**/*"],
|
||||||
|
"exclude": ["node_modules", "dist"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Importing the Module
|
||||||
|
|
||||||
|
With TypeScript configured, import the module into your project using ESM syntax:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { XInvoice } from '@fin.cx/xinvoice';
|
||||||
|
```
|
||||||
|
|
||||||
|
### Working with Invoices
|
||||||
|
|
||||||
|
The core functionality revolves around creating and managing XInvoice instances, which represent your invoices. Let's dive into how we can create and manipulate these instances.
|
||||||
|
|
||||||
|
#### Creating an XInvoice Instance
|
||||||
|
|
||||||
|
To create a new instance of `XInvoice`, you'll need a PDF path, an XML path (which contains the invoice data), and an output path where the processed file will be saved:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const pdfPath = './path/to/your/invoice.pdf';
|
||||||
|
const xmlPath = './path/to/your/invoice.xml';
|
||||||
|
const outputPath = './path/to/save/processed_invoice.pdf';
|
||||||
|
|
||||||
|
const xInvoice = new XInvoice(pdfPath, xmlPath, outputPath);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Embedding XML into a PDF
|
||||||
|
|
||||||
|
One of the key features is embedding XML data into a PDF. This is crucial for ensuring that your invoice contains all necessary data in a format that's both human-readable and machine-processable. Here’s how to embed XML into a PDF:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await xInvoice.embedXml();
|
||||||
|
```
|
||||||
|
|
||||||
|
This method asynchronously processes the PDF and XML files, embedding the XML data into the PDF. Upon completion, it saves the modified PDF to the specified `outputPath`.
|
||||||
|
|
||||||
|
### Parsing XML to Create an Invoice Object
|
||||||
|
|
||||||
|
While the provided example doesn't directly expose a parsing method, `@fin.cx/xinvoice` is designed to facilitate the extension and customization of such functionalities. If parsing from XML to a structured invoice object is necessary, consider implementing a parser that utilizes the interfaces provided by the module.
|
||||||
|
|
||||||
|
For illustrative purposes, if there was a method `parseXmlToInvoice(xmlContent: string)` within `XInvoice`, it could be used like this:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const xmlContent = `<Your XML content here>`;
|
||||||
|
const invoiceData = xInvoice.parseXmlToInvoice(xmlContent);
|
||||||
|
|
||||||
|
console.log(invoiceData);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Conclusion
|
||||||
|
|
||||||
|
The `@fin.cx/xinvoice` module provides a foundational framework for working with xinvoices, focusing on embedding XML data into PDFs and facilitating the potential parsing of XML to structured invoice data. Its usage in a TypeScript environment ensures type safety and enhances the development experience.
|
||||||
|
|
||||||
|
This guide covered installing the module, setting up a TypeScript project to use ESM syntax, creating an `XInvoice` instance, and embedding XML into PDFs. With these capabilities, `@fin.cx/xinvoice` is a robust tool for managing xinvoice packages within your application.
|
||||||
|
|
||||||
|
## License and Legal Information
|
||||||
|
|
||||||
|
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||||
|
|
||||||
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
### Trademarks
|
||||||
|
|
||||||
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||||
|
|
||||||
|
### Company Information
|
||||||
|
|
||||||
|
Task Venture Capital GmbH
|
||||||
|
Registered at District court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
|
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
10
test/test.ts
Normal file
10
test/test.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { tap, expect } from '@push.rocks/tapbundle';
|
||||||
|
|
||||||
|
import * as xinvoice from '../ts/index.js';
|
||||||
|
|
||||||
|
tap.test('XInvoice should correctly embed XML into a PDF', async (tools) => {
|
||||||
|
const xInvoice = new xinvoice.XInvoice('');
|
||||||
|
await xInvoice.embedXml();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start(); // Run the test suite
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@fin.cx/xinvoice',
|
||||||
|
version: '1.0.4',
|
||||||
|
description: 'A module for creating, manipulating, and embedding XML data within PDF files for xinvoice packages.'
|
||||||
|
}
|
115
ts/classes.xinvoice.ts
Normal file
115
ts/classes.xinvoice.ts
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
import * as plugins from './plugins.js';
|
||||||
|
import * as interfaces from './interfaces.js';
|
||||||
|
import { PDFDocument, PDFDict, PDFName, PDFRawStream } from 'pdf-lib';
|
||||||
|
|
||||||
|
export class XInvoice {
|
||||||
|
public pdfUint8Array: Uint8Array;
|
||||||
|
|
||||||
|
constructor(pdfBuffer: Uint8Array | Buffer) {
|
||||||
|
this.pdfUint8Array = Uint8Array.from(pdfBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async embedXml(xmlString: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
|
||||||
|
const pdfDoc = await PDFDocument.load(this.pdfUint8Array);
|
||||||
|
|
||||||
|
const xmlBuffer = new TextEncoder().encode(xmlString);
|
||||||
|
pdfDoc.attach(xmlBuffer, plugins.path.basename('invoice.xml'), {
|
||||||
|
mimeType: 'application/xml',
|
||||||
|
description: 'XRechnung XML Invoice',
|
||||||
|
});
|
||||||
|
|
||||||
|
const modifiedPdfBytes = await pdfDoc.save();
|
||||||
|
this.pdfUint8Array = modifiedPdfBytes;
|
||||||
|
console.log(`PDF Buffer updated with new XML attachment!`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error embedding XML into PDF:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads the xml part and returns parsed structured data
|
||||||
|
* @returns {Promise<interfaces.IXInvoice>} Parsed XML data
|
||||||
|
*/
|
||||||
|
public async getParsedXmlData(): Promise<interfaces.IXInvoice> {
|
||||||
|
try {
|
||||||
|
const pdfBuffer = this.pdfUint8Array;
|
||||||
|
const pdfDoc = await PDFDocument.load(pdfBuffer);
|
||||||
|
|
||||||
|
const namesDict = pdfDoc.catalog.get(PDFName.of('Names')) as PDFDict;
|
||||||
|
if (!namesDict) throw new Error('No Names dictionary found in PDF!');
|
||||||
|
|
||||||
|
const embeddedFilesDict = namesDict.get(PDFName.of('EmbeddedFiles')) as PDFDict;
|
||||||
|
if (!embeddedFilesDict) throw new Error('No EmbeddedFiles dictionary found!');
|
||||||
|
|
||||||
|
const filesSpecDict = embeddedFilesDict.get(PDFName.of('Names')) as PDFDict;
|
||||||
|
if (!filesSpecDict) throw new Error('No files specified in EmbeddedFiles dictionary!');
|
||||||
|
|
||||||
|
let xmlFile: PDFRawStream | undefined = undefined;
|
||||||
|
const entries = filesSpecDict.entries();
|
||||||
|
for (const [key, fileSpecValue] of entries) {
|
||||||
|
const fileSpec = fileSpecValue as PDFDict;
|
||||||
|
const efDict = fileSpec.get(PDFName.of('EF')) as PDFDict;
|
||||||
|
xmlFile = efDict.get(PDFName.of('F')) as PDFRawStream;
|
||||||
|
if (xmlFile) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!xmlFile) throw new Error('XML file stream not found!');
|
||||||
|
|
||||||
|
const xmlBytes = xmlFile.getContents();
|
||||||
|
const xmlContent = new TextDecoder().decode(xmlBytes);
|
||||||
|
console.log(`Read embedded XML: ${xmlContent}`);
|
||||||
|
|
||||||
|
return this.parseXmlToInvoice(xmlContent);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error extracting or parsing embedded XML from PDF:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseXmlToInvoice(xmlContent: string): interfaces.IXInvoice {
|
||||||
|
// Implement XML parsing logic here
|
||||||
|
// Placeholder return, replace with actual parsing result
|
||||||
|
return {
|
||||||
|
InvoiceNumber: '12345',
|
||||||
|
DateIssued: '2023-04-01',
|
||||||
|
Seller: {
|
||||||
|
Name: 'Seller Co',
|
||||||
|
Address: {
|
||||||
|
Street: '1234 Market St',
|
||||||
|
City: 'Sample City',
|
||||||
|
PostalCode: '12345',
|
||||||
|
Country: 'DE',
|
||||||
|
},
|
||||||
|
Contact: {
|
||||||
|
Email: 'contact@sellerco.com',
|
||||||
|
Phone: '123-456-7890',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Buyer: {
|
||||||
|
Name: 'Buyer Inc',
|
||||||
|
Address: {
|
||||||
|
Street: '5678 Trade Rd',
|
||||||
|
City: 'Trade City',
|
||||||
|
PostalCode: '67890',
|
||||||
|
Country: 'DE',
|
||||||
|
},
|
||||||
|
Contact: {
|
||||||
|
Email: 'info@buyerinc.com',
|
||||||
|
Phone: '987-654-3210',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Items: [
|
||||||
|
{
|
||||||
|
Description: 'Item 1',
|
||||||
|
Quantity: 10,
|
||||||
|
UnitPrice: 9.99,
|
||||||
|
TotalPrice: 99.9,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
TotalAmount: 99.9,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
7
ts/index.ts
Normal file
7
ts/index.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import * as interfaces from './interfaces.js';
|
||||||
|
|
||||||
|
export {
|
||||||
|
interfaces,
|
||||||
|
}
|
||||||
|
|
||||||
|
export * from './classes.xinvoice.js';
|
33
ts/interfaces.ts
Normal file
33
ts/interfaces.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
export interface IXInvoice {
|
||||||
|
InvoiceNumber: string;
|
||||||
|
DateIssued: string; // Date in ISO 8601 format
|
||||||
|
Seller: IParty;
|
||||||
|
Buyer: IParty;
|
||||||
|
Items: IInvoiceItem[];
|
||||||
|
TotalAmount: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IParty {
|
||||||
|
Name: string;
|
||||||
|
Address: IAddress;
|
||||||
|
Contact: IContact;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IAddress {
|
||||||
|
Street: string;
|
||||||
|
City: string;
|
||||||
|
PostalCode: string;
|
||||||
|
Country: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IContact {
|
||||||
|
Email: string;
|
||||||
|
Phone: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IInvoiceItem {
|
||||||
|
Description: string;
|
||||||
|
Quantity: number;
|
||||||
|
UnitPrice: number;
|
||||||
|
TotalPrice: number;
|
||||||
|
}
|
20
ts/plugins.ts
Normal file
20
ts/plugins.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// node native
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
export {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
|
// @push.rocks scope
|
||||||
|
import * as smartfile from '@push.rocks/smartfile';
|
||||||
|
|
||||||
|
export {
|
||||||
|
smartfile
|
||||||
|
}
|
||||||
|
|
||||||
|
// third party
|
||||||
|
import * as pdfLib from 'pdf-lib';
|
||||||
|
|
||||||
|
export {
|
||||||
|
pdfLib
|
||||||
|
}
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user