Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
How do I preview a pdf file in my <img> tag before uploading it into the server?

This is my HTML code:

<div class="mb-3" style="width: 50%; margin: 0 auto; border: 2px solid white;">
     <label for="file">Select your snippet file :</label><br>
     <input type="file" id="myfile" name="myfile" accept=".pdf,.jpg,.png">
</div>
<div class="container">
     <span id="preview_text" style="text-align: center; vertical-align: middle; line-height: 200px; color: red;">No preview available</span>
     <img id="scanOutput" src="#" style="height: 200px;"/>
</div>

This is my javascript:

myfile.onchange = evt => {
    const [file] = myfile.files
    if (file) {
        if(file.type=="image/jpeg" || file.type=="image/png"){
            if(document.getElementById("preview_text")!=null)
                document.getElementById("preview_text").remove();
            if((file.size/1024)<200){
                scanOutput.src = URL.createObjectURL(file);
                document.getElementsByClassName("container")[0].style.border="2px solid green";
            }
            else{
                scanOutput.src="#";
                if(document.getElementById("preview_text")==null){
                    var elem=document.createElement("SPAN");
                    elem.id="preview_text";
                    elem.innerText="File size should not be greater than 200kb";
                    elem.style="text-align: center; vertical-align: middle; line-height: 200px; color: red;";
                    document.getElementsByClassName("container")[0].appendChild(elem);
                }
                document.getElementsByClassName("container")[0].style.border="2px solid red";
            }
        }
        else if(file.type=="application/pdf"){
            //how do I preview the pdf into my <img> field?
        }
    }
}

I can preview the jpg/png images in my <img> tag, but not the pdf. I want to also be able to preview the pdf files in my img field. How do I do that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't use a HTML <img> tag to preview a .pdf file as souce. The proper way is to embed the .pdf via <iframe> or <embed> tag.

For more information on this please refer to: Recommended way to embed PDF in HTML?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!