Skip to content

Commit

Permalink
Merge pull request #29 from joshua-gould/setTransform
Browse files Browse the repository at this point in the history
added setTransform.js and test
  • Loading branch information
joshua-gould authored Sep 28, 2023
2 parents c35f00f + 880cf02 commit 996d677
Show file tree
Hide file tree
Showing 32 changed files with 51,323 additions and 1,114 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: test
on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
node-version: [ 16.x ]
node-version: [16.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -22,9 +22,10 @@ jobs:
run: |
sudo apt-get update && sudo apt-get install -y graphicsmagick libxss1
yarn install
- name: Test with yarn
- name: Run tests
run: |
yarn test
yarn test:generate-images
yarn test:convert-pdfs
yarn test:diff
env:
CI: true

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
# Canvas2PDF
# Canvas2PDF

Canvas2PDF exports your HTML canvas as PDF using JavaScript.
Note that this library generates actual PDF drawing calls to create a PDF with vector graphics,
Canvas2PDF exports your HTML canvas as PDF using JavaScript.
Note that this library generates actual PDF drawing calls to create a PDF with vector graphics,
unlike some alternate libraries which rasterize your canvas and place it as an image in your PDF.


## Usage

```javascript
import PdfContext from '/src/canvas2pdf';
import blobStream from 'blob-stream';
import { saveAs } from 'file-saver';
import PdfContext from "/src/canvas2pdf";
import blobStream from "blob-stream";
import { saveAs } from "file-saver";

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

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

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

## Interactive Browser Demo
[Open Demo](https://joshua-gould.github.io/canvas2pdf/dist/index.html)

[Open Demo](https://joshua-gould.github.io/canvas2pdf/dist/index.html)

## Notes
+ Calling fill and then stroke consecutively only executes fill
+ Some canvas 2d context methods are not implemented yet (e.g. setTransform and arcTo)

- Calling fill and then stroke consecutively only executes fill
- Some canvas 2d context methods are not implemented yet (e.g. arcTo)

## License

MIT

# Developer Dependencies
+ GraphicsMagick is required for running tests

- GraphicsMagick is required for running tests
27 changes: 13 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import PdfContext from '/src/canvas2pdf';
import blobStream from 'blob-stream';
import PdfContext from "/src/canvas2pdf";
import blobStream from "blob-stream";

const editor = document.getElementById('editor_source');
const editor = document.getElementById("editor_source");

const examplePicker = document.getElementById('example_picker');
examplePicker.onchange = function() {
const examplePicker = document.getElementById("example_picker");
examplePicker.onchange = function () {
editor.textContent = document.getElementById(examplePicker.value).textContent;
createPdf();
};

const iframe = document.querySelector('iframe');
const createPdf = function() {
const iframe = document.querySelector("iframe");
const createPdf = function () {
const text = editor.textContent;

const stream = blobStream();
const ctx = new PdfContext(stream);

ctx.stream.on('finish', function() {
iframe.src = ctx.stream.toBlobURL('application/pdf');
ctx.stream.on("finish", function () {
iframe.src = ctx.stream.toBlobURL("application/pdf");
});
eval(text);
};
;
document.getElementById('redraw').addEventListener(
'click',
function() {
document.getElementById("redraw").addEventListener(
"click",
function () {
createPdf();
},
false
false,
);

createPdf();
2 changes: 2 additions & 0 deletions dist/canvas2pdf.js

Large diffs are not rendered by default.

Loading

0 comments on commit 996d677

Please sign in to comment.