Giter Site home page Giter Site logo

adrianhajdin / tutorial_pdf_generation Goto Github PK

View Code? Open in Web Editor NEW
117.0 4.0 83.0 197 KB

This is a code repository for the corresponding article on Medium. In this tutorial, you will learn how to generate dynamic PDFs using HTML code as a template.

HTML 5.34% CSS 6.44% JavaScript 88.23%
tutorial-code article

tutorial_pdf_generation's Introduction

Generate Dynamic PDFs Using React and NodeJS

Currency Converter Tutorial

This is a code repository for the corresponding article on Medium.

In this tutorial, you will learn how to generate dynamic PDFs using HTML code as a template.

Project Setup

  1. Create a new directory mkdir pdfGenerator && cd pdfGenerator
  2. Create a new React App with create-react-app client and then move into newly created directory and install dependencies cd client && npm i -S axios file-saver
  3. Create an Express server with mkdir server && cd server && touch index.js && npm init press enter a couple of times to initialize package.json and then run npm i -S express body-parser cors html-pdf to save all the necessary dependencies.
  4. Add proxy inside of client/package.json, above the dependencies, simply add “proxy”: “http://localhost:5000/", so you can call the localhost from the client.
  5. Open two different terminals: First one: go into the client directory and run npm start Second one: go into the server directory and run nodemon index.js

tutorial_pdf_generation's People

Contributors

adrianhajdin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tutorial_pdf_generation's Issues

Concurrent file generation

Not sure because I haven't tested this yet, but it seems that this may get some bugs with high concurrency, because files are always being generated with the same name, please tell me if I am wrong. Maybe a way to fix this intuitive problem is to generate a random filename on creation, send to before fetching. Please tell me what do you think about this, have you tested the concurrency on this?

 createAndDownloadPdf = () => {
    axios.post('/create-pdf', this.state)
      .then((filename) => axios.get('fetch-pdf', { responseType: 'blob', filename}))
      .then((res) => {
        const pdfBlob = new Blob([res.data], { type: 'application/pdf' });

        saveAs(pdfBlob, 'newPdf.pdf');
      })
  }


app.post('/create-pdf', (req, res) => {
    const rndFilename = randomfilename(); /* you can implement this as you wish maybe using a your favourite hashfuncion*/
    pdf.create(pdfTemplate(req.body), {}).toFile( rndFilename  , (err, rndFilename ) => {
        if(err) {
            res.send(Promise.reject());
        }

        res.send(Promise.resolve(rndFilename));
    });
});

app.get('/fetch-pdf', (req, res) => {
    res.sendFile(`${__dirname}/${res.filename}`)
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.