Skip to content
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

Small improvements to the PDF model #6538

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion panel/models/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class JSON(Markup):

class PDF(Markup):

embed = Bool(True, help="Whether to embed the file")
embed = Bool(False, help="Whether to embed the file")

start_page = Int(default=1, help="Start page of the pdf, by default the first page.")
36 changes: 15 additions & 21 deletions panel/models/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ export class PDFView extends PanelMarkupView {

override connect_signals(): void {
super.connect_signals()
const p = this.model.properties
const {text, width, height, embed, start_page} = p
this.on_change([text, width, height, embed, start_page], () => {
this.update()
})
const {text, width, height, embed, start_page} = this.model.properties
this.on_change([text, width, height, embed, start_page], () => { this.update() })
}

override render(): void {
Expand All @@ -24,28 +21,26 @@ export class PDFView extends PanelMarkupView {
if (this.model.embed) {
const blob = this.convert_base64_to_blob()
const url = URL.createObjectURL(blob)
const w = this.model.width || "100%"
const h = this.model.height || "100%"
this.container.innerHTML = `<embed src="${url}#page=${this.model.start_page}" type="application/pdf" width="${w}" height="${h}"></embed>`
this.container.innerHTML = `<embed src="${url}#page=${this.model.start_page}" type="application/pdf" width="100%" height="100%"></embed>`
} else {
const html = htmlDecode(this.model.text)
this.container.innerHTML = html || ""
}
}

protected convert_base64_to_blob(): Blob {
const byteCharacters = atob(this.model.text)
const sliceSize = 512
const byteArrays = []
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize)
const byteNumbers = new Uint8Array(slice.length)
const byte_characters = atob(this.model.text)
const slice_size = 512
const byte_arrays = []
for (let offset = 0; offset < byte_characters.length; offset += slice_size) {
const slice = byte_characters.slice(offset, offset + slice_size)
const byte_numbers = new Uint8Array(slice.length)
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i)
byte_numbers[i] = slice.charCodeAt(i)
}
byteArrays.push(byteNumbers)
byte_arrays.push(byte_numbers)
}
return new Blob(byteArrays, {type: "application/pdf"})
return new Blob(byte_arrays, {type: "application/pdf"})
}
}

Expand All @@ -68,12 +63,11 @@ export class PDF extends Markup {
}

static override __module__ = "panel.models.markup"

static {
this.prototype.default_view = PDFView
this.define<PDF.Props>(({Float, Bool}) => ({
embed: [Bool, true],
start_page: [Float, 1],
this.define<PDF.Props>(({Int, Bool}) => ({
embed: [Bool, false],
start_page: [Int, 1],
}))
}
}
Loading