Skip to content

Commit 015cc87

Browse files
authored
Added node 22 support, dropped node 18 support
2 parents 15901f8 + 5c23563 commit 015cc87

19 files changed

+867
-1658
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
os: [windows-latest, ubuntu-latest]
16-
node: ['20', '18']
16+
node: ['20', '22']
1717
steps:
1818
- uses: actions/checkout@v4
1919
- name: Setup .NET

PdfJsSharp/NodeVersionDetector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static int CheckRequiredNodeVersionInstalled(string nodeExecuteablePath,
8282

8383
if (supportedMajorNodeVersions.Any(_ => _ == foundMajorVersion))
8484
{
85-
return foundMajorVersion.Value;
85+
return 22;
8686
}
8787

8888
var expectedVersions = string.Join(", ", supportedMajorNodeVersions.Select(_ => _.ToString(CultureInfo.InvariantCulture)));

PdfJsSharp/PdfJsSharp.csproj

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
<ItemGroup>
4747
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
48-
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.12.0.78982">
48+
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.6.0.109712">
4949
<PrivateAssets>all</PrivateAssets>
5050
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
5151
</PackageReference>
@@ -68,19 +68,15 @@
6868

6969

7070
<ItemGroup>
71-
<None Remove="node_modules.linux.node20.zip" />
72-
<None Remove="node_modules.linux.node18.zip" />
73-
<None Remove="node_modules.win.node20.zip" />
74-
<None Remove="node_modules.win.node18.zip" />
7571
<None Remove="Rasterize.mjs" />
72+
<None Remove="node_modules.linux.node22.zip" />
73+
<None Remove="node_modules.win.node22.zip" />
7674
</ItemGroup>
7775

7876
<ItemGroup>
7977
<EmbeddedResource Include="Rasterize.mjs" />
80-
<EmbeddedResource Include="node_modules.win.node20.zip" />
81-
<EmbeddedResource Include="node_modules.linux.node20.zip" />
82-
<EmbeddedResource Include="node_modules.win.node18.zip" />
83-
<EmbeddedResource Include="node_modules.linux.node18.zip" />
78+
<EmbeddedResource Include="node_modules.win.node22.zip" />
79+
<EmbeddedResource Include="node_modules.linux.node22.zip" />
8480
</ItemGroup>
8581

8682

PdfJsSharp/PdfJsWrapper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Codeuctivity.PdfjsSharp
1919
public class PdfJsWrapper : IDisposable
2020
{
2121
internal const int windowsMaxPathLength = 206;
22-
internal static readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
22+
internal static readonly SemaphoreSlim semaphore = new(1, 1);
2323
internal bool useCustomNodeModulePath;
2424
internal string pathToNodeModules = default!;
2525
internal string pathToTempFolder = default!;
@@ -28,7 +28,7 @@ public class PdfJsWrapper : IDisposable
2828
/// <summary>
2929
/// Supported node versions
3030
/// </summary>
31-
private readonly ImmutableArray<int> SupportedNodeVersions = ImmutableArray.Create(18, 20);
31+
private readonly ImmutableArray<int> SupportedNodeVersions = [20, 22];
3232

3333
internal bool IsInitialized { get; set; }
3434

@@ -180,7 +180,7 @@ private void InitializeNodeExecutablePath()
180180

181181
var home = Environment.GetEnvironmentVariable("HOME");
182182

183-
if (home !=null)
183+
if (home != null)
184184
{
185185
var path = Path.Combine(home, ".nvm", "versions", "node");
186186
if (Directory.Exists(path))

PdfJsSharp/Rasterize.mjs

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,87 @@
11
// based on https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png
22
/*global Uint8Array*/
33

4-
import { strict as assert } from "assert";
5-
import Canvas from "canvas";
64
import fs from "fs";
75
import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
86

97
class NodeCanvasFactory {
10-
create(width, height) {
11-
assert(width > 0 && height > 0, "Invalid canvas size");
12-
const canvas = Canvas.createCanvas(width, height);
13-
const context = canvas.getContext("2d");
14-
return {
15-
canvas,
16-
context,
17-
};
18-
}
8+
create(width, height) {
9+
assert(width > 0 && height > 0, "Invalid canvas size");
10+
const canvas = Canvas.createCanvas(width, height);
11+
const context = canvas.getContext("2d");
12+
return {
13+
canvas,
14+
context,
15+
};
16+
}
1917

20-
reset(canvasAndContext, width, height) {
21-
assert(canvasAndContext.canvas, "Canvas is not specified");
22-
assert(width > 0 && height > 0, "Invalid canvas size");
23-
canvasAndContext.canvas.width = width;
24-
canvasAndContext.canvas.height = height;
25-
}
18+
reset(canvasAndContext, width, height) {
19+
assert(canvasAndContext.canvas, "Canvas is not specified");
20+
assert(width > 0 && height > 0, "Invalid canvas size");
21+
canvasAndContext.canvas.width = width;
22+
canvasAndContext.canvas.height = height;
23+
}
2624

27-
destroy(canvasAndContext) {
28-
assert(canvasAndContext.canvas, "Canvas is not specified");
25+
destroy(canvasAndContext) {
26+
assert(canvasAndContext.canvas, "Canvas is not specified");
2927

30-
// Zeroing the width and height cause Firefox to release graphics
31-
// resources immediately, which can greatly reduce memory consumption.
32-
canvasAndContext.canvas.width = 0;
33-
canvasAndContext.canvas.height = 0;
34-
canvasAndContext.canvas = null;
35-
canvasAndContext.context = null;
36-
}
28+
// Zeroing the width and height cause Firefox to release graphics
29+
// resources immediately, which can greatly reduce memory consumption.
30+
canvasAndContext.canvas.width = 0;
31+
canvasAndContext.canvas.height = 0;
32+
canvasAndContext.canvas = null;
33+
canvasAndContext.context = null;
34+
}
3735
}
3836

3937
export async function convertToPng(sourceFile, targetPrefix) {
40-
// Some PDFs need external cmaps.
41-
const CMAP_URL = "../../../node_modules/pdfjs-dist/cmaps/";
42-
const CMAP_PACKED = true;
38+
// Some PDFs need external cmaps.
39+
const CMAP_URL = "../../../node_modules/pdfjs-dist/cmaps/";
40+
const CMAP_PACKED = true;
4341

44-
const canvasFactory = new NodeCanvasFactory();
42+
const canvasFactory = new NodeCanvasFactory();
4543

46-
// Loading file from file system into typed array.
47-
const pdfData = new Uint8Array(fs.readFileSync(sourceFile));
44+
// Loading file from file system into typed array.
45+
const pdfData = new Uint8Array(fs.readFileSync(sourceFile));
4846

49-
// Load the PDF file.
50-
const loadingTask = getDocument({
51-
data: pdfData,
52-
cMapUrl: CMAP_URL,
53-
cMapPacked: CMAP_PACKED,
54-
});
55-
const pdfDocument = await loadingTask.promise;
47+
// Load the PDF file.
48+
const loadingTask = getDocument({
49+
data: pdfData,
50+
cMapUrl: CMAP_URL,
51+
cMapPacked: CMAP_PACKED,
52+
});
53+
const pdfDocument = await loadingTask.promise;
5654

57-
for (let pageNumber = 1; pageNumber <= pdfDocument.numPages; pageNumber++) {
58-
await processPage(pageNumber);
59-
}
60-
return pdfDocument.numPages;
55+
for (let pageNumber = 1; pageNumber <= pdfDocument.numPages; pageNumber++) {
56+
await processPage(pageNumber);
57+
}
58+
return pdfDocument.numPages;
6159

62-
async function processPage(pageNumber) {
63-
console.log("# Processing page:", pageNumber);
64-
// Get the page.
65-
const page = await pdfDocument.getPage(pageNumber);
66-
// Render the page on a Node canvas with 100% scale.
67-
const viewport = page.getViewport({ scale: 1.0 });
68-
const canvasAndContext = canvasFactory.create(
69-
viewport.width,
70-
viewport.height
71-
);
72-
const renderContext = {
73-
canvasContext: canvasAndContext.context,
74-
viewport,
75-
};
60+
async function processPage(pageNumber) {
61+
console.log("# Processing page:", pageNumber);
62+
// Get the page.
63+
const page = await pdfDocument.getPage(pageNumber);
64+
// Render the page on a Node canvas with 100% scale.
65+
const canvasFactory = pdfDocument.canvasFactory;
66+
const viewport = page.getViewport({ scale: 1.0 });
67+
const canvasAndContext = canvasFactory.create(
68+
viewport.width,
69+
viewport.height
70+
);
71+
const renderContext = {
72+
canvasContext: canvasAndContext.context,
73+
viewport,
74+
};
7675

77-
const renderTask = page.render(renderContext);
78-
await renderTask.promise;
79-
// Convert the canvas to an image buffer.
80-
const image = canvasAndContext.canvas.toBuffer();
81-
const targetFile = `${targetPrefix}${pageNumber}.png`;
82-
83-
fs.writeFileSync(targetFile, image);
84-
console.log("Finished converting page", pageNumber, "to", targetFile);
85-
// Release page resources.
86-
page.cleanup();
87-
}
88-
}
76+
const renderTask = page.render(renderContext);
77+
await renderTask.promise;
78+
// Convert the canvas to an image buffer.
79+
const image = canvasAndContext.canvas.toBuffer("image/png");
80+
const targetFile = `${targetPrefix}${pageNumber}.png`;
8981

82+
fs.writeFileSync(targetFile, image);
83+
console.log("Finished converting page", pageNumber, "to", targetFile);
84+
// Release page resources.
85+
page.cleanup();
86+
}
87+
}

PdfJsSharp/Rasterizer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Jering.Javascript.NodeJS;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Reflection;
54
using System.Threading.Tasks;
65

76
namespace Codeuctivity.PdfjsSharp
-57.1 MB
Binary file not shown.
-31.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)