Skip to content

Commit

Permalink
Bump Pdfjs 3.6.172 -> 4.0.189
Browse files Browse the repository at this point in the history
  • Loading branch information
stesee authored Nov 12, 2023
2 parents 814fe11 + 3fd5ede commit 4f4dc08
Show file tree
Hide file tree
Showing 16 changed files with 924 additions and 700 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cla.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
steps:
- name: "CLA Assistant"
if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA') || github.event_name == 'pull_request_target'
uses: cla-assistant/github-action@v2.3.0
uses: cla-assistant/github-action@v2.3.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# the below token should have repo scope and must be manually added by you in the repository's secret
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:
os: [windows-latest, ubuntu-latest]
node: ['20', '18']
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Restore dependencies
Expand All @@ -35,7 +35,7 @@ jobs:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
Expand Down
10 changes: 6 additions & 4 deletions PdfJsSharp/PdfJsSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<Product>PdfjsSharp</Product>
<AssemblyName>Codeuctivity.PdfjsSharp</AssemblyName>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
<PackageId>Codeuctivity.PdfjsSharp</PackageId>
<PackageReadmeFile>nugetReadme.md</PackageReadmeFile>
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
Expand All @@ -39,12 +41,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Jering.Javascript.NodeJS" Version="6.3.1" />
<PackageReference Include="Jering.Javascript.NodeJS" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.0.0.68202">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.12.0.78982">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -71,11 +73,11 @@
<None Remove="node_modules.linux.node18.zip" />
<None Remove="node_modules.win.node20.zip" />
<None Remove="node_modules.win.node18.zip" />
<None Remove="Rasterize.js" />
<None Remove="Rasterize.mjs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Rasterize.js" />
<EmbeddedResource Include="Rasterize.mjs" />
<EmbeddedResource Include="node_modules.win.node20.zip" />
<EmbeddedResource Include="node_modules.linux.node20.zip" />
<EmbeddedResource Include="node_modules.win.node18.zip" />
Expand Down
5 changes: 3 additions & 2 deletions PdfJsSharp/PdfJsWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand Down Expand Up @@ -123,7 +124,7 @@ public async Task InitPdfJsWrapper()

await ExtractBinaryFromManifest($"Codeuctivity.PdfjsSharp.node_modules.win.node{foundVersion}.zip").ConfigureAwait(false);

pathToNodeModules = pathToTempFolder.Replace("\\", "/") + "/node_modules/";
pathToNodeModules = "file://" + pathToTempFolder.Replace("\\", "/") + "/node_modules/";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Expand Down Expand Up @@ -184,7 +185,7 @@ private void InitializeNodeExecutablePath()
{
var installedNodeVersions = Directory.GetDirectories(path);

var nodeExecutableDirectory = installedNodeVersions.FirstOrDefault(directory => SupportedNodeVersions.Any(version => Path.GetFileName(directory).StartsWith("v" + version.ToString())));
var nodeExecutableDirectory = installedNodeVersions.FirstOrDefault(directory => SupportedNodeVersions.Any(version => Path.GetFileName(directory).StartsWith("v" + version.ToString(CultureInfo.InvariantCulture), true, CultureInfo.InvariantCulture)));

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / deployRelease

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

Check warning on line 188 in PdfJsSharp/PdfJsWrapper.cs

View workflow job for this annotation

GitHub Actions / deployRelease

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

if (nodeExecutableDirectory != null)
{
Expand Down
82 changes: 0 additions & 82 deletions PdfJsSharp/Rasterize.js

This file was deleted.

96 changes: 96 additions & 0 deletions PdfJsSharp/Rasterize.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// based on https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png
/*global Uint8Array*/

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

class NodeCanvasFactory {
create(width, height) {
assert(width > 0 && height > 0, "Invalid canvas size");
const canvas = Canvas.createCanvas(width, height);
const context = canvas.getContext("2d");
return {
canvas,
context,
};
}

reset(canvasAndContext, width, height) {
assert(canvasAndContext.canvas, "Canvas is not specified");
assert(width > 0 && height > 0, "Invalid canvas size");
canvasAndContext.canvas.width = width;
canvasAndContext.canvas.height = height;
}

destroy(canvasAndContext) {
assert(canvasAndContext.canvas, "Canvas is not specified");

// Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
canvasAndContext.canvas.width = 0;
canvasAndContext.canvas.height = 0;
canvasAndContext.canvas = null;
canvasAndContext.context = null;
}
}

export async function convertToPng(sourceFile, targetPrefix) {
// Loading file from file system into typed array.
const data = new Uint8Array(fs.readFileSync(sourceFile));

// Some PDFs need external cmaps.
const CMAP_URL = "../../../node_modules/pdfjs-dist/cmaps/";
const CMAP_PACKED = true;

// Where the standard fonts are located.
const STANDARD_FONT_DATA_URL =
"../../../node_modules/pdfjs-dist/standard_fonts/";

const canvasFactory = new NodeCanvasFactory();

// Loading file from file system into typed array.
const pdfData = new Uint8Array(fs.readFileSync(sourceFile));

// Load the PDF file.
const loadingTask = getDocument({
data: pdfData,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
});
const pdfDocument = await loadingTask.promise;

for (let pageNumber = 1; pageNumber <= pdfDocument.numPages; pageNumber++) {
await processPage(pageNumber);
}
return pdfDocument.numPages;

async function processPage(pageNumber) {
console.log("# Processing page:", pageNumber);
// Get the page.
const page = await pdfDocument.getPage(pageNumber);
// Render the page on a Node canvas with 100% scale.
const viewport = page.getViewport({ scale: 1.0 });
const canvasAndContext = canvasFactory.create(
viewport.width,
viewport.height
);
const renderContext = {
canvasContext: canvasAndContext.context,
viewport,
};

const renderTask = page.render(renderContext);
await renderTask.promise;
// Convert the canvas to an image buffer.
const image = canvasAndContext.canvas.toBuffer();
const targetFile = `${targetPrefix}${pageNumber}.png`;

fs.writeFileSync(targetFile, image);
console.log("Finished converting page", pageNumber, "to", targetFile);
// Release page resources.
page.cleanup();
}
}

Loading

0 comments on commit 4f4dc08

Please sign in to comment.