skip to Main Content

I am trying to embed a pdf within an iframe but when I set height to 100% it is really small.
Is there a way to make the height exactly one page?

my code

<iframe src="/wp-content/uploads/test.pdf#view=FitH" width="100%" height="100%"></iframe>

5

Answers


  1. You need to set the following settings in CSS (suitable for PDF page size):
    width="594px"
    height="580px"

    Login or Signup to reply.
  2. Try to add some CSS into your code as below:

    <iframe src="/wp-content/uploads/test.pdf#view=FitH" style="width: 100%; height: 100vh"></iframe>
    
    Login or Signup to reply.
  3. There are two parts to the question

    Part 1

    "when I set height="100%" the frame is too small
    it will be 150px on whatever device since you cannot use % for frame height. The correct way is to set a frame to 100vh (viewport height as suggested by Baris Taskiran in their answer) but there are frame imbedding values that suggest say style as width: calc(100vw - 18px)!important; min-height: calc(100vh - 18px)!important ; can be preferable to avoid drag resizing issues.
    see https://stackoverflow.com/a/74354395/10802527

    Part 2

    You cannot force a browser internal PDF view (it is after PDF download, or not, or download and view. Embed, iFrame or Object it makes no difference the PDF is out of your control and in the application/PDF) but you may suggest it attempt to FitV (fit the vertical) in the viewers downloaded frame.

    However that can be meaningless for some PDF viewing plugins, if they are not Acrobat since those are Adobe Acrobat "fragments" and do not need to be supported by plugin extensions such as Chromes Foxit/Skia or Firefoxs PDF.js etc.

    For more on the topic see https://stackoverflow.com/a/72265519/10802527 and https://stackoverflow.com/a/72106063/10802527

    The question asked is why the middle FitV appears not to be working and since both the HTML and the PDF now belong to the user, they may edit or control view as they wish. This allows users to change font if they wish to inverted W&B Comic Sans or allow for different screen sizes/dpi etc. Both the files are 100% theirs.

    enter image description here

    Login or Signup to reply.
  4. The following answer does NOT work:

    =====

    This may not be practical for you, but this is how I guarantee that only one page of a pdf is displayed in an .

    Give your a class–I use "x85x110" for 8.5" x 11" paper and use the following CSS:

    iframe.x85x110 { height:calc(103% * (8.5 / 11)); }

    The "103%" is a fudge factor that you can change to get exactly the height you want, i.e., to get just a hair of the blank space between one page and the next. The white-space on either side of the "*" and "/" is critical–calc won’t work without it.

    =====

    This answer, however, DOES work:

    =====

    First the CSS:

    iframe.x85x110 { width:80%; }

    Second, the HTML:

    <iframe class="x85x110" src="A Boy And His Dog/docs/Downunder Expansion - 8.5 x 11 - 10pt.pdf"></iframe>

    Third, the javascript:

    <script type="text/javascript" >
    
        x85x110();
        window.addEventListener('resize', x85x110);
        
        function x85x110()
        {
            array_x85x110 = document.getElementsByClassName("x85x110");
            
            for (count=0; count<array_x85x110.length; count++)
            {
                array_x85x110[count].style.height = Math.round(array_x85x110[count].offsetWidth * (11.0 / 8.5)) + "px";
                
            } // for (count=0; count<array_x85x110.length; count++)
        } // function x85x110()
    

    Fourth, the explanation:

    Give your <iframe> a class name, I used x85x110 because my documents are 8.5" x 11".

    I use width:80% because I want the frame to be 80% of the width of the column width in which the .pdf and its containing <iframe> lives–this is NOT necessary.

    The x85x110(); calls the function x85x110() when the page loads.

    The window.addEventListener('resize', x85x110); calls the x85x110() function whenever the page is resized.

    The array_x85x110 = document.getElementsByClassName("x85x110"); collects all of the <iframe> elements of the class x85x110 in an array named array_x85x110.

    Then, for each element of array_x85x110, i.e., for each <iframe> of the class x85x110, we loop using the for (count=0; count<array_x85x110.length; count++) {} and set the height of the <iframe> to (11.0 /8.5) times the offsetWidth of the <iframe> with the the:

    array_x85x110[count].style.height = Math.round(array_x85x110[count].offsetWidth * (11.0 / 8.5)) + "px";
    

    Math.round() rounds the resizing of the <iframe>‘s height to the nearest pixel.

    The (11.0 / 8.5) divides the <iframe>‘s .offsetWidth by 8.5 (the width of my pdf page in inches), which changes as the browser window is resized, and multiplies by 11 (the height of my pdf page in inches), to maintain the pdf’s natural aspect ratio.

    If for some bizarre reason you’re using A4 paper, you European wierdo :-), the (11.0 / 8.5) would be (11.69 / 8.27), i.e., the height of a piece of A4 paper divided by its width.)

    =====

    You can see this CSS at work, for real now at my board game design page.

    Sorry for the confusion.

    Login or Signup to reply.
  5. Try CSS Aspect Ratio

    Collaborating with a few answers here, seems like the addition of the css property "aspect-ratio:8.5/11" will get the right fit and works for me for letterheads. I encourage you to try other ratios for your specific pdf dimension needs.

    Combination of parameter: #view=FitV (Chrome), #zoom=FitH (Firefox), aspect-ratio:8.5/11, and some other css to make to fit to viewport with calc()’s to account for scrollbars.

    The below was broken into several lines to become easier to read. Write your production code on one line or use the TDLR version below that.

    This code seems to be responsive, Shrinking your browser window adjusts the height since it is using auto, and the aspect-ratio keeps it the correct proportions. If you encounter and issues or fixes let me know.

    Example below is for Chrome, I suggest using browser detection to change the parameter based useragent data. There are plenty of methods you can use to browser detect, if you are using php checkout
    https://github.com/foroco/php-browser-detection that collects the data into a clean array $_SERVER[‘HTTP_USER_AGENT’];

    <iframe
    src="<?php echo $pdf_link ?>#view=FitV"
    width="95vw"
    height="auto" 
    style="
    width:calc(95vw - 18px)!important;
    height:auto;
    aspect-ratio:8.5/11; 
    "></iframe>
    

    TLDR C+P

    Not a reader? here you go…

    <iframe src="<?php echo $pdf_link ?>#view=FitV" width="95vw" height="auto" style="width:calc(95vw - 18px)!important;height:auto!important;aspect-ratio:8.5/11"></iframe>
    

    CSS Aspect Ratio

    https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio

    Cool resource for additional parameters to use when opening pdfs

    https://pdfobject.com/pdf/pdf_open_parameters_acro8.pdf#page=6

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search