fix(pdf): reuse content width function
All checks were successful
Open PR on Staging / audit_and_check (pull_request) Successful in 33s

This commit is contained in:
enes 2024-08-28 11:41:29 +02:00
parent a442e71087
commit 59c3fc69a2
2 changed files with 20 additions and 19 deletions

View File

@ -1,29 +1,12 @@
import { useEffect, useState } from 'react'
import { singletonHook } from 'react-singleton-hook'
import { getInnerContentWidth } from '../utils/pdf'
const noScaleInit = {
to: (_: number, v: number) => v,
from: (_: number, v: number) => v
}
const getInnerContentWidth = () => {
// Fetch the first container element we find
const element = document.querySelector('#content-preview')
if (element) {
const style = getComputedStyle(element)
// Calculate width without padding
const widthWithoutPadding =
element.clientWidth - parseFloat(style.padding) * 2
return widthWithoutPadding
}
// Default value
return 620
}
const useScaleImpl = () => {
const [width, setWidth] = useState(getInnerContentWidth())

View File

@ -60,6 +60,24 @@ export const readPdf = (file: File): Promise<string | ArrayBuffer> => {
})
}
export const getInnerContentWidth = () => {
// Fetch the first container element we find
const element = document.querySelector('#content-preview')
if (element) {
const style = getComputedStyle(element)
// Calculate width without padding
const widthWithoutPadding =
element.clientWidth - parseFloat(style.padding) * 2
return widthWithoutPadding
}
// Default value
return 620
}
/**
* Converts pdf to the images
* @param data pdf file bytes
@ -70,7 +88,7 @@ export const pdfToImages = async (
const pages: PdfPage[] = []
const pdf = await PDFJS.getDocument(data).promise
const canvas = document.createElement('canvas')
const width = document.querySelector('#content-preview > *')?.clientWidth || 1
const width = getInnerContentWidth()
for (let i = 0; i < pdf.numPages; i++) {
const page = await pdf.getPage(i + 1)