Jump to content

Preview a PDF before It is Uploaded in a DataPage


Recommended Posts

Hi there. I was trying to find a way to preview a PDF file before it is uploaded in a DataPage. The flow described in this article works for images only: https://howto.caspio.com/tech-tips-and-articles/advanced-customizations/preview-an-image-before-it-is-uploaded-in-a-datapage/

Here is a solution I came up with:

<script>

const HTMLFileInput = document.getElementById("EditRecordFile_1");

HTMLFileInput.addEventListener("change", event => {
const src = URL.createObjectURL(event.target.files[0]);
const outputWidth = window.getComputedStyle(HTMLFileInput.parentElement.parentElement).getPropertyValue('width')
const outputHeight = parseInt(outputWidth.replace('px',''))*1.3+'px'

if(document.querySelector('#output')==null) {
HTMLFileInput.parentElement.insertAdjacentHTML('afterend', `<iframe src="${src}" width="${outputWidth}" height="${outputHeight }" id="output" class="cbFormFieldCell">IFRAME IS NOT SUPPORTED IN YOUR BROWSER</iframe>`)
}
else {
document.querySelector('#output').setAttribute("src", src)
}
})

</script>


Just substitute EditRecordFile_1 with id of your file input. You can also tweak the value of width and the height of the iframe so it fits your usual pdf file size.

Hope this helps
 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...