question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Responsive Document/Page. Mission accomplished, But with a hack... Other solutions?

See original GitHub issue

Before you start - checklist

  • I have read documentation in README
  • I have checked sample and test suites to see real life basic implementation
  • I have checked if this question is not already asked

What are you trying to achieve? Please describe.

I would like to make a responsive Document, meaning that if I resize the window, the document and its pages would also resize. I know it’s possible, and tried to mimic what is done in the sample file, but It’s very possible that I missing something, or maybe I have a CSS rule conflicting maybe.

Describe solutions you’ve tried

I was able to achieve what I wanted to accomplish, but I had to apply a css rule to my html and body tag. The successful result is available on my website, on this link Resume PDF, and its whole code is open source here: Sandro Portfolio.

On desktop, and mobile, the rendering is good, as we can see below: screen shot 2019-01-22 at 12 00 46 am

Its currently working, thanks to this css rule added to html and body:

html, body {
  margin: 0;
  padding: 0;
  overflow-x: hidden;
}

If I remove overflow-x: hidden;, then its rendering the PDF, but it creates a huge white space on the right side, completely outside of the body output. It’s not even the html tag, and the whole screen is scrollable horizontally. This white space is there only for small screen (mobile or very small window). I have no clue what creates that behavior, and I was hoping that it would be possible to achieve this without setting that rule: overflow-x: hidden;. The sample doesn’t seem to do it either.

Here a screenshot of the result when the overflow rule is removed: screen shot 2019-01-22 at 12 04 03 am

Why is that behavior happening? It seems like the PDF is breaking my html / body content. I would assume that it wouldn’t impact the parents (div / component).

Additional information The Resume page code is all in the folder of the component Resume on the repo here.

ResumePage.js

import React, { Component } from 'react';

import { Document, Page, pdfjs } from 'react-pdf/dist/entry.webpack';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

import './ResumePage.css';
import 'react-pdf/dist/Page/AnnotationLayer.css';

import pdfFile from '../../public/SandroResume.pdf';

class ResumePage extends Component {
    state = {
        file: pdfFile,
        numPages: null,
        pageNumber: 1,
    }

    onDocumentLoadSuccess = ({ numPages }) => {
        this.setState({ numPages });
    }

    render() {
        const { file, pageNumber } = this.state;
        return (
            <div id="ResumeContainer">
                <Document className={"PDFDocument"} file={file} onLoadSuccess={this.onDocumentLoadSuccess}>
                    <Page className={"PDFPage PDFPageOne"} pageNumber={pageNumber} renderTextLayer={false} renderInteractiveForms={false} />
                    <Page className={"PDFPage"} pageNumber={pageNumber + 1} renderTextLayer={false} renderInteractiveForms={false} />
                </Document>
            </div>
        );
    }
}

export default ResumePage;

ResumePage.css

#ResumeContainer {
    margin:auto;
    width: 65%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.PDFDocument {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.PDFPage {
    box-shadow: 0 0 8px rgba(0, 0, 0, .5);
}

.PDFPageOne {
    margin-bottom: 25px;
}

.PDFPage > canvas {
    max-width: 100%;
    height: auto !important;
}

Question on the fly: Where is the best place for doing that instruction:

import { pdfjs } from 'react-pdf/dist/entry.webpack';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

It’s currently lives in the ResumePage.js. But Im wondering if there is a recommended place to do it? The main component of the React app (App.js) ? Or it’s okay the way I did it?

Environment

  • Browser: Chrome 71.0.3578.98
  • React-PDF version: 4.0.2
  • React version: 16.7.0
  • Webpack version: 4.28.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:17
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
rjhilgefortcommented, Feb 9, 2019

Thanks for this! It might be a hack, but it got the job done for me! 🙏 🙏 🙏

1reaction
pxFINcommented, Feb 27, 2019

FYI

https://github.com/pxFIN/react-router-express-node-example

Check my MyPDF component for one way of doing it (its very rude example without individual page sizes but for resizing purposes)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Breach Response: A Guide for Business
You just learned that your business experienced a data breach. Whether hackers took personal information from your corporate server, an insider stole ...
Read more >
What is an ethical hacker and what does the work entail?
An ethical hacker is a security expert who acts like a malicious intruder to penetrate a computer system to reveal vulnerabilities to the...
Read more >
What to do before and after a cybersecurity breach?
Use this plan to prioritize the efforts required to recover from a cyberattack, understand the extent of the damage, and minimize further damage....
Read more >
How do I deal with a compromised server?
This question keeps being asked repeatedly by the victims of hackers breaking into their web server. The answers very rarely change, but ......
Read more >
Data Breach Response Checklist - Protecting Student Privacy
Establishing a plan for responding to a data breach, complete with clearly defined roles and ... hackers gaining access to data through a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found