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

Is it possible to control the size, margins, and position of an image? #250

Open
hugocalheira opened this issue Apr 4, 2024 · 3 comments

Comments

@hugocalheira
Copy link

hugocalheira commented Apr 4, 2024

I need to upscale an image that I'm printing. How do I do this?

import { ThermalPrinter, PrinterTypes, CharacterSet, BreakLine } from 'node-thermal-printer'
import driver from 'printer'
import axios from 'axios'

const printersList = {
  TMT88V: 'printer:EPSON TM-T88V Receipt'
}

let printer = new ThermalPrinter({
  type: PrinterTypes.EPSON,
  interface: printersList.TMT88V,
  driver,
  characterSet: CharacterSet.PC860_PORTUGUESE,
  removeSpecialCharacters: false,
  breakLine: BreakLine.NONE,
  options:{
    timeout: 5000
  },
  width: 42
});

// this function returns me the image that will be printed
async function getLayout() {
  const url = 'URL'
  const payload = {"canal":1,"cdFilial":1,"cdOperador":0,"idCliente":1}
  const result = await axios.post(url, payload, { responseType: 'arraybuffer' })
  await printFromBuffer(result.data)
}

async function printFromBuffer(buffer) {
  try {
    let isConnected = await printer.isPrinterConnected()
    if (!isConnected) {
      throw Error("not connected")
    }
    
    await printer.printImageBuffer(buffer)
    printer.cut()
    await printer.execute()
    printer.clear()
  } catch (e) {
    console.log(e)
  }
}

getLayout()

The image size is 450x1098px and it prints almost perfectly, but it doesn't occupy all the area on the paper.
Mídia (3)

@adnanlah
Copy link

adnanlah commented Apr 6, 2024

I don't think you can with this package.

You can check out the issue I posted few days ago #249. In my case I had to print images in 580px width in order of making the image to take full width of the paper (80mm) on my printer as I didn't try other printers yet.

Try to print images that has 580px width and see if it's common across printers or not. If it worked then you have to use some node.js package to upscale/downscale images to 580px width before printing it.

I would be happy if you let me know how did it go.

Good luck.

@hugocalheira
Copy link
Author

I solved it in a similar way. I upscaled by intercepting the image buffer, changing the width, and generating a new buffer for printing. That way, I got the result I needed in this case. The issue is that depending on the desired size, the final printing speed is significantly impacted, making the process slower.

But I miss having a better way to handle printing, including positioning, scaling, or even controlling print margins.

@adnanlah
Copy link

adnanlah commented Apr 8, 2024

changing the width

Good job you made it work. Would you please help me with the image width that was enough to make the image occupy all width of the paper?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants