goodcom-web-printer
v1.0.1
Published
Goodcom Web Printer SDK
Downloads
198
Readme
Goodcom Web Printer SDK
This project is an SDK for web applications to connect to Goodcom printers via WebSocket and send printing commands. It encapsulates the underlying WebSocket communication, providing simple APIs for printing HTML, PDF, images.
Features
- Provides APIs for sending various print jobs.
- Supports two common web project integration methods:
<script>tag andnpmmodule.
Quick Start
You can integrate the SDK into your project using either of the following methods, depending on your project type.
Method 1: Using <script> Tag
Suitable for static HTML pages or projects without build tools.
Include the SDK in your
index.htmlfile, either in the<head>or at the bottom of the<body>:<script src="https://www.igoodcom.com/sdk/goodcom-printer-sdk.umd.cjs"></script>After inclusion, the SDK will expose a global variable window.GoodcomPrinterSDK. You can destructure it to get the GcPrinter instance class:
const { GcPrinter } = window.GoodcomPrinterSDK; document.getElementById('myButton').onclick = async () => { try { await GcPrinter.printBase64Image(base64String); console.log("successfully"); } catch (e) { console.error("Failed to connect to printer:", e.message); } };
Method 2: Using npm (Recommended)
Suitable for modern front-end projects using npm/yarn and bundlers (like Vite, Webpack).
Install the
goodcom-web-printerpackage:npm install goodcom-web-printerImport the required modules in your JavaScript or TypeScript file:
import { GcPrinter } from 'goodcom-web-printer'; /** For older Node.js environments that do not support ES Modules. const { GcPrinter } = require('goodcom-web-printer'); */ async function handlePrint() { try { await GcPrinter.printBase64Image(base64String); console.log("Print successful"); } catch (e) { console.error("Print failed:", e.message); } }
API Documentation
1. GcPrinter (Main Printer Instance)
The SDK exports a default, instantiated GcPrinter object, which connects to ws://localhost:62769.
Methods (all methods are asynchronous and return a Promise):
printHtml(htmlString: string): Sends an HTML string to the printer for rendering and printing.- htmlString: A string containing HTML tags.
printPdf(file: File): Sends a PDF File object for printing.- file: A File object, typically from an input type="file".
printBase64Image(base64Image: string): Sends a Base64-encoded image string for printing.- base64Image: The pure Base64 string, without the data:image/...;base64, prefix.
