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

Bwip-js and Cloudflare Workers - suggestion to include additional information to documentation #333

Open
m-muiznieks opened this issue Apr 20, 2024 · 1 comment

Comments

@m-muiznieks
Copy link

At first - thank you for your work.

I struggled to find a good solution for implementing the barcode and QR code in the Node.js environment. I thought I can forget about everything after finding this library unless I moved part of my project to the Cloudflare Workers environment.

Well.. nothing worked anymore.

It just threw issues, that .toBuffer is not a function and that's all.

In the process of trying to solve this issue, I found some other libraries, that somehow do the job, but there were other issues, most of them related to the same differences from the Node.js and Workers environment.

Here, I will paste my function (if anyone else is looking for the solution):

import bwipjs from 'bwip-js';
import { Buffer } from 'node:buffer';

interface CodeProps {
  codeType?: string;
  codeValue: string;
  barcodeHeightMm?: number;
  height?: number;
  width?: number;
  margin?: number;
  padding?: number;
  showText?: boolean;
  textXalign?: "center" | "offleft" | "left" | "right" | "offright" | "justify";
  textYalign?: "above" | "below" | "center";
  textColor?: string;
  barCodeColor?: string;
  backgroundColor?: string;
}

export const generateBarcodeSRC = async ({
  codeValue,
  codeType = 'code128',
  barcodeHeightMm = 15,
  showText = true,
  textXalign = "center",
  textYalign = "below",
  textColor = "#000000",
  barCodeColor = "#000000",
  backgroundColor = "#ffffff",
}: CodeProps) => {

  const options = {
    bcid: codeType,
    text: codeValue,
    scale: 3,
    height: barcodeHeightMm,
    includetext: showText,
    textxalign: textXalign,
    textyalign: textYalign,
    textcolor: textColor,
    barcolor: barCodeColor,
    backgroundcolor: backgroundColor,
  };

  try {
    // Generate the barcode buffer
    const buffer = bwipjs.toSVG(options);

    const svgBase64 = Buffer.from(buffer).toString('base64');
    const asString = `data:image/svg+xml;base64,${svgBase64}`;
    
    return asString;
    
  } catch (error) {
    console.error('Error generating barcode:', error);
    return { error: JSON.stringify((error as Error).message), status: 500 };
  }
}

Everything worked before well unless it came to const buffer = bwipjs.toBuffer(options);, where the error appeared.
Instead, I changed it to const buffer = bwipjs.toSVG(options); to output just an SVG.

It's important, to include additional import { Buffer } from 'node:buffer'; at the top (see cloudflare documentation: https://developers.cloudflare.com/workers/runtime-apis/nodejs/), so this const svgBase64 = Buffer.from(buffer).toString('base64'); does not throw another error. :)

--

That's working for me.

For the authors - I wanted you to suggest including additional sentences about the Workers, as at one moment I had the feeling, that I was the only one who is dealing with that. At one place I just read the sentence "Well, just don't do this to workers, it's not for everything".

Again - Thank you. :)

@m-muiznieks m-muiznieks changed the title Bwipjs and Cloudflare Workers - suggestion to include additional information to documentation Bwip-js and Cloudflare Workers - suggestion to include additional information to documentation Apr 20, 2024
@metafloor
Copy link
Owner

Did you look at the examples/threader.js code in the project? It demonstrates how to use with nodejs worker threads. Is there something about the cloudfare environment that is different than the standard node:worker_threads module?

Please tell me more about how you are creating the worker threads and communicating between the main thread and worker threads.

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