-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(Node): pdf/svg output #9185
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
Open
ShaMan123
wants to merge
4
commits into
master
Choose a base branch
from
canvas2pdf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+299
−70
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { Page, TestInfo, expect, test } from '@playwright/test'; | ||
| import { Canvas } from 'canvas'; | ||
| import * as fabric from 'fabric/node'; | ||
| import { createWriteStream, writeFileSync } from 'fs'; | ||
| import { ensureDirSync } from 'fs-extra'; | ||
| import path from 'path'; | ||
|
|
||
| import '../../setup/setupCoverage'; | ||
|
|
||
| test.describe('Exporting node canvas', () => { | ||
| const size = { | ||
| width: 460, | ||
| height: 450, | ||
| }; | ||
|
|
||
| const createCanvas = () => { | ||
| const canvas = new fabric.StaticCanvas(null, { | ||
| width: 1000, | ||
| height: 1000, | ||
| enableRetinaScaling: false, | ||
| }); | ||
| const rect = new fabric.Rect({ width: 50, height: 50, fill: 'red' }); | ||
| const path = fabric.util.getSmoothPathFromPoints([ | ||
| new fabric.Point(50, 50), | ||
| new fabric.Point(100, 100), | ||
| new fabric.Point(50, 200), | ||
| new fabric.Point(400, 150), | ||
| new fabric.Point(500, 500), | ||
| ]); | ||
| const text = new fabric.Text(new Array(9).fill('fabric.js').join(' '), { | ||
| fill: 'blue', | ||
| fontSize: 24, | ||
| path: new fabric.Path(path), | ||
| }); | ||
| canvas.add(rect, text); | ||
| return canvas; | ||
| }; | ||
|
|
||
| const createCtxForExport = (type: 'pdf' | 'svg') => { | ||
| const ctx = new Canvas(0, 0, type).getContext('2d'); | ||
| ctx.textDrawingMode = 'glyph'; | ||
| return ctx; | ||
| }; | ||
|
|
||
| test.describe('SVG', () => { | ||
| const attachSVG = async (result: Canvas, testInfo: TestInfo) => { | ||
| ensureDirSync(testInfo.outputDir); | ||
| const pathTo = path.resolve(testInfo.outputDir, 'output.svg'); | ||
| writeFileSync(pathTo, result.toBuffer()); | ||
| await testInfo.attach('output', { | ||
| path: pathTo, | ||
| }); | ||
| }; | ||
|
|
||
| const testSVG = async (page: Page, result: Canvas) => { | ||
| await page.goto( | ||
| `data:image/svg+xml,${encodeURIComponent(result.toBuffer().toString()) | ||
| .replace(/'/g, '%27') | ||
| .replace(/"/g, '%22')}`, | ||
| { waitUntil: 'load' } | ||
| ); | ||
| expect( | ||
| await page.screenshot({ | ||
| clip: { x: 0, y: 0, width: 460, height: 450 }, | ||
| }) | ||
| ).toMatchSnapshot(); | ||
| }; | ||
|
|
||
| test('canvas', async ({ page }, testInfo) => { | ||
| const ctx = createCtxForExport('svg'); | ||
| const result = createCanvas().toCanvasElement(1, size, ctx); | ||
| expect(result).toMatchObject(size); | ||
| await attachSVG(result, testInfo); | ||
| await testSVG(page, result); | ||
| }); | ||
|
|
||
| test('object', async ({ page }, testInfo) => { | ||
| const ctx = createCtxForExport('svg'); | ||
| const result = createCanvas() | ||
| .item(1) | ||
| .toCanvasElement({ ...size, ctx }); | ||
| expect(result).toMatchObject(size); | ||
| await attachSVG(result, testInfo); | ||
| await testSVG(page, result); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe('PDF', () => { | ||
| const attachPDF = async (result: Canvas, testInfo: TestInfo) => { | ||
| ensureDirSync(testInfo.outputDir); | ||
| const pathTo = path.resolve(testInfo.outputDir, 'output.pdf'); | ||
| const out = createWriteStream(pathTo); | ||
| await new Promise((resolve) => { | ||
| out.on('finish', resolve); | ||
| result.createPDFStream().pipe(out); | ||
| }); | ||
| await testInfo.attach('output', { | ||
| path: pathTo, | ||
| contentType: 'application/pdf', | ||
| }); | ||
| }; | ||
|
|
||
| const testPDF = async (page: Page, result: Canvas) => { | ||
| await page.goto( | ||
| `data:application/pdf;base64,${Buffer.from(result.toBuffer()).toString( | ||
| 'base64' | ||
| )}`, | ||
| { waitUntil: 'load' } | ||
| ); | ||
| await page.waitForTimeout(5000); | ||
| expect( | ||
| await page.screenshot({ | ||
| clip: { x: 80, y: 25, width: 500, height: 500 }, | ||
| }) | ||
| ).toMatchSnapshot(); | ||
| }; | ||
|
|
||
| test('canvas', async ({ page }, testInfo) => { | ||
| const ctx = createCtxForExport('pdf'); | ||
| const result = createCanvas().toCanvasElement(1, size, ctx); | ||
| expect(result).toMatchObject(size); | ||
| await attachPDF(result, testInfo); | ||
| await testPDF(page, result); | ||
| }); | ||
|
|
||
| test('object', async ({ page }, testInfo) => { | ||
| const ctx = createCtxForExport('pdf'); | ||
| const result = createCanvas() | ||
| .item(1) | ||
| .toCanvasElement({ ...size, ctx }); | ||
| expect(result).toMatchObject(size); | ||
| await attachPDF(result, testInfo); | ||
| await testPDF(page, result); | ||
| }); | ||
| }); | ||
| }); |
Binary file added
BIN
+32 KB
...port-node-canvas/index.spec.ts-snapshots/Exporting-node-canvas-PDF-canvas-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.9 KB
...port-node-canvas/index.spec.ts-snapshots/Exporting-node-canvas-PDF-object-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.3 KB
...port-node-canvas/index.spec.ts-snapshots/Exporting-node-canvas-SVG-canvas-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.2 KB
...port-node-canvas/index.spec.ts-snapshots/Exporting-node-canvas-SVG-object-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to me it doens't make sense to specify a ctx here.
Can we releage this to a specific node item?
If we need to export something in PDF/SVG using node-canvas, shouldn't we add a method that is specific to that and return something related to that?
We don't have code size issues in node and we are not bound to bundling issues.
Having toCanvasElement that return a canvas element that is good for pdf/svg export buy only if we previously created a specific ctx sounds complicated.
Let's have a toNodePDF and a toNodeSVG ( or whatever it can be called to avoid confusion with current toSVG ) in the node classes.
Take also care that moving away from node-canvas is still a remote option and if there are other libraries that do not require building or that are more canvas compliant, is not granted they will have a pdf or svg export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is why I didn't expose toPDF etc. So we are not bound to node canvas.
Suggest a different way?