Skip to content

Commit

Permalink
[feat] add background param
Browse files Browse the repository at this point in the history
  • Loading branch information
Milesssssss committed Oct 28, 2024
1 parent a9ab9f5 commit 30bc8c0
Show file tree
Hide file tree
Showing 24 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ temp
yarn.lock
__diff_output__
.jest*

.idea
6 changes: 5 additions & 1 deletion src/canvasFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export class NodeCanvasFactory {
create(width: number, height: number): NonNullableFactory {
invariant(width > 0 && height > 0, "Invalid canvas size");
const canvas = Canvas.createCanvas(width, height);
const context = canvas.getContext("2d");
const context = canvas.getContext("2d", { alpha: true });

// 确保 canvas 背景是透明的
context.clearRect(0, 0, width, height);

return {
canvas,
context,
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as pdfjs from "pdfjs-dist/legacy/build/pdf.mjs";
import type { DocumentInitParameters } from "pdfjs-dist/types/src/display/api.js";
import { NodeCanvasFactory } from "./canvasFactory.js";
import { parseInput } from "./parseInput.js";
import { CanvasGradient, CanvasPattern } from "canvas";

Check warning on line 8 in src/index.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

`canvas` import should occur before import of `./canvasFactory.js`

Check warning on line 8 in src/index.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

`canvas` import should occur before import of `./canvasFactory.js`

Check warning on line 8 in src/index.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

`canvas` import should occur before import of `./canvasFactory.js`

const pdfjsPath = path.dirname(
createRequire(import.meta.url).resolve("pdfjs-dist/package.json")
Expand Down Expand Up @@ -39,6 +40,7 @@ export type Options = {
password?: string;
/** defaults to `1`. If you want high-resolution images, increase this */
scale?: number;
background?: string | CanvasGradient | CanvasPattern | undefined;
/** document init parameters which are passed to pdfjs.getDocument */
docInitParams?: Partial<DocumentInitParameters>;
};
Expand Down Expand Up @@ -101,13 +103,13 @@ export async function pdf(
viewport.width,
viewport.height
);

await page.render({
canvasContext: context,
viewport,
background: options.background,
}).promise;

return canvas.toBuffer();
return canvas.toBuffer("image/png");
}

return {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 30bc8c0

Please sign in to comment.