Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda