PDF API (dte-sdk/pdf)
The printed samples: the PDF417 stamp and the PDF as the SII manual asks. Node only, because the batch functions read and write files. For how to produce and review the samples, see Printed samples.
import { parsePrintableDocuments, renderPrintedSample } from 'dte-sdk/pdf'Peer dependencies#
pdf-lib (^1.17.1) and bwip-js (^4.11.4) are optional peer dependencies. Install them in your project; the SDK loads them on first use, not at import. Without them, the render functions throw DEPENDENCY_UNAVAILABLE. A failure inside the PDF library while drawing is PDF_RENDER_FAILED.
npm install pdf-lib bwip-jsThe printed samples have not been reviewed by the SII yet. They are tested against synthetic data. See Status.
The functions that read or write files take a trailing load parameter that exists only so the SDK's own tests can replace node:fs; it is left out of the signatures below.
Parsing#
Turns any EnvioDTE, EnvioBOLETA, bare SetDTE or bare DTE XML into printable documents, with each TED copied verbatim.
// The printable documents of an XML: an `EnvioDTE`, `EnvioDTEB`, `EnvioDTETraslado` or
// `EnvioBOLETA` envelope, a bare `SetDTE`, or a bare `DTE` (what `SignedDocument.xml` holds). Any
// other root yields an empty list.
function parsePrintableDocuments(xml: string): readonly PrintableDocument[]| Type | Description |
|---|---|
PrintableAdjustment | A global discount or surcharge. |
PrintableDocument | What the printed sample needs of a document, parsed from its XML. Strings are empty and numbers undefined when the tag is absent. |
PrintableIssuer | The issuer of the printed document. |
PrintableItem | A detail line. |
PrintableParty | A party of the printed document, as written in the XML. |
PrintableReference | A reference to another document. |
PrintableTotals | The totals that print; absent ones are undefined. |
Fields of PrintableAdjustment
| Field | Type | Description |
|---|---|---|
kind | 'D' | 'R' | D discount or R surcharge. |
valueType | '%' | '$' | % or $. |
value? | number | Value in that unit. |
Fields of PrintableDocument
| Field | Type | Description |
|---|---|---|
type | number | Document type; TD of the TED when IdDoc lacks it; 0 when neither has it. |
folio | string | Folio; F of the TED as fallback. |
issueDate | string | Issue date as written (YYYY-MM-DD); FE of the TED as fallback. |
transferReason | number | null | Transfer reason of a dispatch guide, null when absent. |
grossAmounts | boolean | Line amounts include VAT. |
issuer | PrintableIssuer | The issuer, with its branch. |
receiver | PrintableParty | The receiver. |
totals | PrintableTotals | The five totals that print. |
globalAdjustments | readonly PrintableAdjustment[] | Global discounts and surcharges. |
items | readonly PrintableItem[] | Detail lines. |
references | readonly PrintableReference[] | References to other documents. |
tedXml | string | The <TED>…</TED> substring of the input, verbatim; empty when none. |
Fields of PrintableIssuer
| Field | Type | Description |
|---|---|---|
rut | string | RUT as written. |
name | string | Legal name. |
activity | string | Line of business. |
address | string | Street address. |
commune | string | Commune. |
branch | string | Branch, empty when none. |
Fields of PrintableItem
| Field | Type | Description |
|---|---|---|
code | string | Item code (the first when there are several). |
name | string | Item name. |
quantity? | number | Quantity. |
unit? | string | Unit of measure. |
unitPrice? | number | Unit price. |
amount? | number | Line amount. |
discountAmount? | number | Line discount amount. |
discountPercent? | number | Line discount percent. |
Fields of PrintableParty
| Field | Type | Description |
|---|---|---|
rut | string | RUT as written. |
name | string | Legal name. |
activity | string | Line of business. |
address | string | Street address. |
commune | string | Commune. |
Fields of PrintableReference
| Field | Type | Description |
|---|---|---|
documentType | string | Referenced document type, as written (SET and non-DTE codes included). |
folio | string | Referenced folio. |
date | string | Referenced date. |
reason | string | Reason. |
Fields of PrintableTotals
| Field | Type | Description |
|---|---|---|
net? | number | Net amount. |
exempt? | number | Exempt amount. |
vat? | number | VAT. |
vatRate? | number | VAT rate. |
total? | number | Total. |
Rendering#
One single-page PDF per document, and the PDF417 stamp alone as a PNG. RenderPrintedSampleOptions adds transferable to the options shared by every sample.
// The PDF417 stamp of a TED as a PNG: error correction level 5, the TED bytes exactly as they were
// signed. The renderer embeds it under the totals; callers with a layout of their own use it
// directly.
function renderTedBarcode(tedXml: string): Promise<Uint8Array>
// True when the printed document carries a transferable copy: 33, 34, 43 and 46 always; 52 only
// when the transfer is a sale (reason 1 or 9, 1 assumed when absent), as the manual excludes
// transfers that are not sales; never for notes or receipts.
function needsTransferableCopy(doc: PrintableDocument): boolean
// The printed sample of one document as a single-page PDF per the SII printed-samples manual:
// issuer and the red type box, issue date, receiver, transfer reason (guides), references, detail,
// totals, the acknowledgement box and the `CEDIBLE` mark on a transferable copy, and the PDF417
// stamp with its legends.
function renderPrintedSample(
doc: PrintableDocument,
options?: RenderPrintedSampleOptions,
): Promise<Uint8Array>| Type | Description |
|---|---|
PrintedLogo | A logo for the top left of the sample. |
PrintedSampleOptions | Options shared by every printed sample. |
RenderPrintedSampleOptions | Options of renderPrintedSample. |
Fields of PrintedLogo
| Field | Type | Description |
|---|---|---|
bytes | Uint8Array | PNG or JPEG bytes. |
format | 'png' | 'jpeg' | Image format. |
Fields of PrintedSampleOptions
| Field | Type | Description |
|---|---|---|
siiOffice? | string | SII office under the type box; default the office of the issuer's commune (CmnaOrigen), by siiOfficeForCommune, or S.I.I. - SANTIAGO CENTRO when the table lacks the commune. |
resolution? | string | Resolution line under the stamp; default Res. Ex. SII N° 0 del {year}. |
logo? | PrintedLogo | Logo at the top left; one the library cannot embed is ignored. |
now? | () => Date | Clock for the year of the default resolution. |
Fields of RenderPrintedSampleOptions
| Field | Type | Description |
|---|---|---|
siiOffice? | string | SII office under the type box; default the office of the issuer's commune (CmnaOrigen), by siiOfficeForCommune, or S.I.I. - SANTIAGO CENTRO when the table lacks the commune. |
resolution? | string | Resolution line under the stamp; default Res. Ex. SII N° 0 del {year}. |
logo? | PrintedLogo | Logo at the top left; one the library cannot embed is ignored. |
now? | () => Date | Clock for the year of the default resolution. |
transferable? | boolean | Draw the transferable copy (acknowledgement box and CEDIBLE mark) when the document admits one; default false. |
SII offices#
The table behind the default siiOffice: the office of each commune, keyed by the legend printed under the type box. Both exports are pure and need neither pdf-lib nor bwip-js.
import { SII_OFFICES, siiOfficeForCommune } from 'dte-sdk/pdf'
siiOfficeForCommune(' ÑUÑOA ') // 'S.I.I. - ÑUÑOA'
siiOfficeForCommune('Nunoa') // 'S.I.I. - ÑUÑOA'
siiOfficeForCommune('Atlantis') // null// The SII office of each commune: 355 commune names in 40 offices, every official commune included.
// Keys are the legend printed under the type box (`S.I.I. - {office}`); values are the communes
// that office serves, in lowercase, without accents and with single spaces.
const SII_OFFICES: Readonly<Record<string, readonly string[]>>
// The legend for a commune (the issuer's `CmnaOrigen`), or null when the table does not have it.
// Case, surrounding spaces and accents are ignored.
function siiOfficeForCommune(commune: string): string | nullThe printed samples use it when siiOffice is not given: they print siiOfficeForCommune(issuer.commune), and S.I.I. - SANTIAGO CENTRO only for a commune the table lacks.
Batches in memory#
Renders every document of every envelope in memory, into the test and simulation sets. Failures are reported as data and the batch goes on.
// Renders every document of every envelope, in memory: one sample per document and, with
// `transferable`, a second one for each document that admits a transferable copy.
function renderPrintedSamples(input: PrintedSamplesInput): Promise<PrintedSamplesResult>| Type | Description |
|---|---|
PrintedEnvelope | An envelope to print: a name (the file name, the artefact key) and its XML. |
PrintedSample | One rendered sample. |
PrintedSampleFailure | One envelope or document that could not be rendered. |
PrintedSampleFile | A sample file as the certification uploads it: its base name and its bytes. |
PrintedSampleSet | The two sets of printed samples the certification asks for. Defined as 'tests' | 'simulation'. |
PrintedSamplesInput | Input of renderPrintedSamples. |
PrintedSamplesResult | Result of renderPrintedSamples. |
Fields of PrintedEnvelope
| Field | Type | Description |
|---|---|---|
name | string | Name; it decides the set. |
xml | string | The envelope XML. |
Fields of PrintedSample
| Field | Type | Description |
|---|---|---|
name | string | muestra_{type}_{folio}.pdf, with _cedible before the extension on a transferable copy. |
set | PrintedSampleSet | The set the sample belongs to. |
source | string | The envelope name. |
type | number | Document type. |
folio | string | Folio. |
transferable | boolean | True on the transferable copy. |
bytes | Uint8Array | The PDF bytes. |
Fields of PrintedSampleFailure
| Field | Type | Description |
|---|---|---|
source | string | The envelope name. |
type? | number | Absent when the envelope itself failed to parse. |
folio? | string | Folio of the document that failed. |
transferable? | boolean | True when the transferable copy failed. |
code? | DteErrorCode | The DteError code, when it was one. |
reason | string | The error message. |
Fields of PrintedSampleFile
| Field | Type | Description |
|---|---|---|
fileName | string | The file name, [A-Za-z0-9_.-]+.pdf. |
bytes | Uint8Array | The PDF bytes. |
Fields of PrintedSamplesInput
| Field | Type | Description |
|---|---|---|
envelopes | readonly PrintedEnvelope[] | The envelopes to render, each with a name. |
options? | PrintedSampleOptions | Rendering options shared by every sample. |
transferable? | boolean | Also produce the transferable copy of every document that admits one; default false. |
Fields of PrintedSamplesResult
| Field | Type | Description |
|---|---|---|
success | boolean | True when nothing failed. |
samples | readonly PrintedSample[] | The samples rendered. |
failures | readonly PrintedSampleFailure[] | The envelopes and documents that failed. |
documents | number | Documents parsed. |
tests | number | Samples in the test set. |
simulation | number | Samples in the simulation set. |
Files#
The same batch over files: find the envelope files, write the PDFs under SET-PRUEBAS and SET-SIMULACION, and read them back for the upload to the samples portal (uploadPrintedSamples in dte-sdk/portal).
// The envelope files under a path: the file itself, or every `.xml` under a directory
// (recursively) whose name contains `envio-set-`, `envio-simulacion` or `envioboleta`, so the
// responses and receipt envelopes of the exchange, which carry no DTE, are skipped.
function findEnvelopeFiles(inputPath: string): Promise<readonly string[]>
// Every `.pdf` under a directory, recursively, in name order at every level, so the test set's
// folder (`SET-PRUEBAS`) comes before the simulation's (`SET-SIMULACION`) as `writePrintedSamples`
// lays them out; each with its base name and its bytes, ready for the printed samples step.
function readPrintedSamples(dir: string): Promise<readonly PrintedSampleFile[]>
// Renders the samples of every file into `{outDir}/SET-PRUEBAS` and `{outDir}/SET-SIMULACION`, one
// PDF per sample under its sample name.
function writePrintedSamples(input: WritePrintedSamplesInput): Promise<WritePrintedSamplesResult>| Type | Description |
|---|---|
WritePrintedSamplesInput | Input of writePrintedSamples. |
WritePrintedSamplesResult | Result of writePrintedSamples. |
WrittenPrintedSample | A sample written to disk. |
Fields of WritePrintedSamplesInput
| Field | Type | Description |
|---|---|---|
xmlFiles | readonly string[] | Envelope XML files. |
outDir | string | Output directory; SET-PRUEBAS and SET-SIMULACION are created inside it. |
options? | PrintedSampleOptions | Rendering options shared by every sample. |
logoPath? | string | Logo file, read once; replaces options.logo. A missing file or an extension other than png, jpg or jpeg means no logo. |
transferable? | boolean | Also write the transferable copies; default false. |
Fields of WritePrintedSamplesResult
| Field | Type | Description |
|---|---|---|
documents | number | Documents parsed. |
simulation | number | Samples in the simulation set. |
tests | number | Samples in the test set. |
success | boolean | True when nothing failed. |
failures | readonly PrintedSampleFailure[] | The envelopes and documents that failed. |
samples | readonly WrittenPrintedSample[] | The samples written, with path and size. |
Fields of WrittenPrintedSample
| Field | Type | Description |
|---|---|---|
folio | string | Folio. |
type | number | Document type. |
name | string | muestra_{type}_{folio}.pdf, with _cedible before the extension on a transferable copy. |
set | PrintedSampleSet | The set the sample belongs to. |
source | string | The envelope name. |
transferable | boolean | True on the transferable copy. |
path | string | Path of the PDF. |
size | number | Size in bytes. |