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

How to get it to work without nodejs #30

Open
abdurahmanshiine opened this issue Oct 4, 2023 · 1 comment
Open

How to get it to work without nodejs #30

abdurahmanshiine opened this issue Oct 4, 2023 · 1 comment

Comments

@abdurahmanshiine
Copy link

abdurahmanshiine commented Oct 4, 2023

Hi there,

I'm trying to use this module without nodejs, but I'm getting an error telling me PdfContext is undefined. Here is my code for reference:

(index.html)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="blob-stream.js"></script>
    <script src="file-saver.min.js"></script>
    <script src="canvas2pdf.min.js"></script>
    <script src="./index.js"></script>
</head>

<body>
...
</body>

</html>

The file canvas2pdf.min.js above is the /dist/canvas2pdf.js file

(index.js)

// Create a new PDF canvas context.
const ctxPdf = new PdfContext(blobStream());

// draw your canvas like you would normally
ctxPdf.fillStyle = "yellow";
ctxPdf.fillRect(100, 100, 100, 100);
// more canvas drawing, etc...

// convert your PDF to a Blob and save to file
ctxPdf.stream.on("finish", function () {
  var blob = ctxPdf.stream.toBlob("application/pdf");
  saveAs(blob, "example.pdf", true);
});
ctxPdf.end();

And this is the error the browser is throwing Uncaught ReferenceError: PdfContext is not defined

Any idea what I'm doing wrong?

@joshua-gould
Copy link
Owner

I would suggest using a build tool (e.g https://github.com/joshua-gould/canvas2pdf/blob/master/package.json#L27)

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