fix: ship PDFjs with the plugin

This commit is contained in:
Johannes Frohnmeyer 2024-05-16 18:55:48 +02:00
parent 25c9eef01c
commit 3dd21b82b4
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 40 additions and 6 deletions

View File

@ -6,6 +6,7 @@ plugins {
kotlin("jvm") version "1.9.22"
kotlin("plugin.serialization") version "1.9.22"
id("jf.autoversion") version "1.6-SNAPSHOT"
id("de.undercouch.download") version "5.3.0"
}
group = "io.gitlab.jfronny"
@ -34,6 +35,32 @@ dependencies {
}
tasks {
val extraResources = layout.buildDirectory.dir("downloads/resources")
val pdfJsVersion = "3.11.174"
val downloadPdfViewerCss by registering(de.undercouch.gradle.tasks.download.Download::class) {
src("https://cdnjs.cloudflare.com/ajax/libs/pdf.js/$pdfJsVersion/pdf_viewer.min.css")
dest(extraResources.map { it.file("pdf.viewer.css") })
overwrite(false)
}
val downloadPdfViewerJs by registering(de.undercouch.gradle.tasks.download.Download::class) {
src("https://cdnjs.cloudflare.com/ajax/libs/pdf.js/$pdfJsVersion/pdf.min.js")
dest(extraResources.map { it.file("pdf.mjs") })
overwrite(false)
}
val downloadPdfWorkerJs by registering(de.undercouch.gradle.tasks.download.Download::class) {
src("https://cdnjs.cloudflare.com/ajax/libs/pdf.js/$pdfJsVersion/pdf.worker.min.js")
dest(extraResources.map { it.file("pdf.worker.mjs") })
overwrite(false)
}
processResources {
dependsOn(downloadPdfViewerCss, downloadPdfViewerJs, downloadPdfWorkerJs)
}
sourceSets.main {
resources.srcDirs(extraResources)
}
// Set the JVM compatibility versions
withType<JavaCompile> {
sourceCompatibility = "17"

View File

@ -5,7 +5,9 @@ import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.ui.JBColor
import com.intellij.ui.jcef.JBCefScrollbarsHelper
import com.intellij.util.ObjectUtils
import io.gitlab.jfronny.sdom.SDom
import org.intellij.lang.annotations.Language
import java.io.InputStreamReader
import java.util.*
private fun getCssColor(key: ColorKey): String {
@ -16,7 +18,9 @@ private fun getCssColor(key: ColorKey): String {
}
private val backgroundColor = ColorKey.createColorKey("Editor.background", JBColor.WHITE)
private const val pdfJsVersion = "3.11.174"
private val viewerCss = SDom.javaClass.getResourceAsStream("/pdf.viewer.css")!!.use { InputStreamReader(it).use { it.readText() } }
private val pdfJs = SDom.javaClass.getResourceAsStream("/pdf.mjs")!!.use { InputStreamReader(it).use { it.readText() } }
private val pdfWorkerUrl = "data:text/plain;base64," + Base64.getEncoder().encodeToString(SDom.javaClass.getResourceAsStream("/pdf.worker.mjs")!!.use { it.readAllBytes() })
private const val scale = "1.5"
@Language("HTML")
@ -24,7 +28,9 @@ fun pdfBootstrap(pdf: ByteArray) = """
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/$pdfJsVersion/pdf.min.js" type="module"></script>
<script type="module">
$pdfJs
</script>
</head>
<body>
<script type="module">
@ -34,7 +40,7 @@ fun pdfBootstrap(pdf: ByteArray) = """
const { pdfjsLib } = globalThis;
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/$pdfJsVersion/pdf.worker.min.js';
pdfjsLib.GlobalWorkerOptions.workerSrc = '$pdfWorkerUrl';
// Using DocumentInitParameters object to load binary data.
const loadingTask = pdfjsLib.getDocument({data: pdfData});
@ -46,8 +52,7 @@ fun pdfBootstrap(pdf: ByteArray) = """
pdf.getPage(i).then(function(page) {
console.log('Page loaded');
const scale = $scale;
const viewport = page.getViewport({scale: scale});
const viewport = page.getViewport({scale: $scale});
// Prepare canvas using PDF page dimensions
const canvas = document.createElement('canvas');
@ -86,7 +91,9 @@ fun pdfBootstrap(pdf: ByteArray) = """
});
</script>
<div id="our-body" style="width: 100%"></div>
<link rel='stylesheet' type='text/css' href='https://cdnjs.cloudflare.com/ajax/libs/pdf.js/$pdfJsVersion/pdf_viewer.min.css'>
<style>
$viewerCss
</style>
<style>
${JBCefScrollbarsHelper.getOverlayScrollbarsSourceCSS()}
${JBCefScrollbarsHelper.buildScrollbarsStyle()}