https://nodejs.org/api/buffer.html
const buffer1 = Buffer.from(ArrayBuffer)
const buffer2 = Buffer.from(Uint8Array)
https://nodejs.org/api/buffer.html
const buffer1 = Buffer.from(ArrayBuffer)
const buffer2 = Buffer.from(Uint8Array)
Using a promise
const stream2Buffer = async () => {
return new Promise(function (resolve, reject) {
const chunks = []
stream.on('data', chunk => chunks.push(chunk))
stream.on('end', () => resolve(Buffer.concat(chunks)))
stream.on("error", err => reject(err))
})
}
const buffer = await stream2Buffer()
A stream is also iterable (see here), so you can also use for await...of
(example)
const chunks = []
for await (const chunk of stream) {
chunks.push(chunk)
}
const buffer = Buffer.concat(chunks)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
const now = Intl.DateTimeFormat('de', {
dateStyle: 'medium',
timeStyle: 'medium'
}).format( Date.now() )
console.log( now )
When using a blob or base64 data to open a file in PDF.js, the viewer is not able to get the correct filename. So when downloading the pdf using the download button inside the viewer, it will always download as document.pdf
. To manually set a filename, you can use the setTitleUsingUrl
function.
<iframe id="pdf-js-viewer" src="/pdf/web/viewer.html?file=" title="webviewer" frameborder="0" width="100%" height="700" allowfullscreen="" webkitallowfullscreen=""/>
let pdfViewerIFrame = document.getElementById("pdf-js-viewer")
pdfViewerIFrame.contentWindow.PDFViewerApplication.setTitleUsingUrl("myFilename")
More info here: https://github.com/mozilla/pdf.js/issues/10435
Using regex, you can simply reorder the parts of your date string to create a compatible ISO 8601 date string which is accepted by the new Date()
constructor.
const date_string = "20220101"
const oDate = new Date(date_string.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'))
console.log(oDate.toUTCString())
As null
belongs to the JS Falsy Values, you can simply do this:
console.log(null) // null
console.log(!!null) // false
Detailed explanation: https://www.samanthaming.com/tidbits/19-2-ways-to-convert-to-boolean/
Using regex, you can simply reorder the parts of your date string to create a compatible ISO 8601 date string which is accepted by the new Date()
constructor.
const date_string = "01.01.2022"
const oDate = new Date(date_string.replace(/(.*)\.(.*)\.(.*)/, '$3-$2-$1'))
console.log(oDate.toUTCString())
The new PDF.js viewer design Photon supports a light and a dark mode. By default, the theme is set automatically. To overwrite this, you can set the viewerCssTheme property.
I’ve embedded my PDF viewer in an iFrame like this:
<iframe id="pdf-js-viewer" src="/pdf/web/viewer.html" title="webviewer" frameborder="0" width="100%" height="700" allowfullscreen="" webkitallowfullscreen=""/>
By setting the viewerCssTheme property, you are able to change the PDF viewer theme. It can be modified using the PDFViewerApplicationOptions.set()
function and there are three possible values. The property has to be set before the Viewer is initialized. To archive this, you can listen to the event webviewerloaded (read more about it here). In my case, I also had to call _forceCssTheme()
after that.
document.addEventListener("webviewerloaded", async () => {
let pdfViewerIFrame = document.getElementById("pdf-js-viewer")
// https://github.com/mozilla/pdf.js/blob/master/web/app_options.js
pdfViewerIFrame.contentWindow.PDFViewerApplicationOptions.set("viewerCssTheme", 1) // 0=automatic theme, 1=light theme, 2=dark theme
pdfViewerIFrame.contentWindow.PDFViewerApplication._forceCssTheme()
})
With pull request 11789, the PDF Viewer respects the PDF property Content Copying, if the viewer option enablePermissions is set to true.
I’ve embedded my viewer in an iFrame like this:
<iframe id="pdf-js-viewer" src="/pdf/web/viewer.html" title="webviewer" frameborder="0" width="100%" height="700" allowfullscreen="" webkitallowfullscreen=""/>
The enablePermissions property can be modified using the PDFViewerApplicationOptions.set()
function. The property has to be set before the Viewer is initialized. To archive this, you can listen to the event webviewerloaded (read more about it here).
document.addEventListener("webviewerloaded", async () => {
let pdfViewerIFrame = document.getElementById("pdf-js-viewer")
//https://github.com/mozilla/pdf.js/blob/master/web/app_options.js
pdfViewerIFrame.contentWindow.PDFViewerApplicationOptions.set("enablePermissions", true) //allow PDF documents to disable copying in the viewer
pdfViewerIFrame.contentWindow.PDFViewerApplicationOptions.set("defaultUrl", "") //prevent loading default pdf
}
})
I was again working on a task for my hierarchical tree structure (see previous post here). This time I needed to find a specific object with a specific property value.
So here again my nested array:
const aTreeData = [{
"NodeId": 1,
"HierarchyLevel": 1,
"type": "folder",
"nodes": [{
"NodeId": 2,
"HierarchyLevel": 2,
"type": "folder",
"nodes": [{
"NodeId": 3,
"HierarchyLevel": 3,
"type": "category"
}]
},
{
"NodeId": 4,
"HierarchyLevel": 2,
"type": "category",
"nodes": [{
"NodeId": 5,
"HierarchyLevel": 3,
"type": "file"
}]
}
]
},
{
"NodeId": 6,
"HierarchyLevel": 1,
"type": "folder",
"nodes": [{
"NodeId": 7,
"HierarchyLevel": 2,
"type": "category"
}]
}
]
I needed a function that was able to find any object by providing only the NodeId value. Fortunately I stumbled accross some excellent code snippet by Scott Sauyet which is exactly doing what I needed:
I just had to change the properties to match my objects and it was working!
const findNodeInTreeData = (aTreeData, nodeId) => {
const deepFind = pred => ([x, ...xs] = []) =>
x && (pred(x) ? x : deepFind(pred)(x.nodes) || deepFind(pred)(xs))
const findByNodeId = id => obj =>
deepFind(o => o.NodeId == id)([obj])
return findByNodeId(nodeId)(aTreeData[0])
}
console.log(findNodeInTreeData(aTreeData, 5))