Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for jpeg images #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,13 @@ class ThermalPrinter {
async printImage(image) {
try {
// Check if file exists
fs.accessSync(image);
const file = fs.readFileSync(image);

// Check for file type
if (image.slice(-4) === '.png') {
if (
file[0] === 0x89 || // png
file[0] === 0xff // jpeg
) {
try {
const response = await this.printer.printImage(image);
this.append(response);
Expand All @@ -346,7 +349,7 @@ class ThermalPrinter {
throw error;
}
} else {
throw new Error('Image printing supports only PNG files.');
throw new Error('Image printing supports only PNG and JPEG files.');
}
} catch (error) {
throw error;
Expand Down
9 changes: 3 additions & 6 deletions lib/types/epson.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,10 @@ class Epson extends PrinterType {

// ----------------------------------------------------- PRINT IMAGE -----------------------------------------------------
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=88
async printImage(image) {
const fs = require('fs');
const { PNG } = require('pngjs');
async printImage(imagePath) {
try {
const data = fs.readFileSync(image);
const png = PNG.sync.read(data);
const buff = this.printImageBuffer(png.width, png.height, png.data);
const image = this.getImageData(imagePath);
const buff = this.printImageBuffer(image.width, image.height, image.data);
return buff;
} catch (error) {
throw error;
Expand Down
27 changes: 27 additions & 0 deletions lib/types/printer-type.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const {PNG} = require("pngjs");
const jpeg = require("jpeg-js");

class PrinterType {
constructor() {
console.error('Consturctor not set');
Expand Down Expand Up @@ -42,6 +45,30 @@ class PrinterType {
console.error(new Error("'printImageBuffer' not implemented yet"));
return null;
}

getImageData(imagePath) {
const fs = require('fs');

let image = null;

const file = fs.readFileSync(imagePath);
if(!file){
throw new Error(`Could not read image ${imagePath}.`);
}

// Detect if file is jpg
if (file[0] === 0xff) {
image = jpeg.decode(file);
} else {
image = PNG.sync.read(file);
}

if (image == null) {
throw new Error("Could not read image.");
}

return image;
}
}

module.exports = PrinterType;
9 changes: 3 additions & 6 deletions lib/types/star.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,10 @@ class Star extends PrinterType {
}

// ----------------------------------------------------- PRINT IMAGE -----------------------------------------------------
async printImage(image) {
const fs = require('fs');
const { PNG } = require('pngjs');
async printImage(imagePath) {
try {
const data = fs.readFileSync(image);
const png = PNG.sync.read(data);
const buff = this.printImageBuffer(png.width, png.height, png.data);
const image = this.getImageData(imagePath);
const buff = this.printImageBuffer(image.width, image.height, image.data);
return buff;
} catch (error) {
throw error;
Expand Down
9 changes: 3 additions & 6 deletions lib/types/tanca.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,10 @@ class Tanca extends PrinterType {

// ----------------------------------------------------- PRINT IMAGE -----------------------------------------------------
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=88
async printImage(image) {
const fs = require('fs');
const { PNG } = require('pngjs');
async printImage(imagePath) {
try {
const data = fs.readFileSync(image);
const png = PNG.sync.read(data);
const buff = this.printImageBuffer(png.width, png.height, png.data);
const image = this.getImageData(imagePath);
const buff = this.printImageBuffer(image.width, image.height, image.data);
return buff;
} catch (error) {
throw error;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"homepage": "https://github.com/Klemen1337/node-thermal-printer",
"dependencies": {
"iconv-lite": "0.5.0",
"jpeg-js": "^0.4.4",
"pngjs": "3.3.3",
"unorm": "1.4.1",
"write-file-queue": "0.0.1"
Expand Down