Giter VIP home page Giter VIP logo

pdf-display-and-rotation's Introduction

PreRequisites:

  • Recommended library to render/display pdf pages is, react-pdf. You can also use anyother pdf library as well in order to render your pdf.

  • You can also use iframe tag to render the url that the below functions will return.

  • For complete Project which includes react-pdf implementation as well. Please follow this github:

Github/MuzammilIrshad

Getting Started

Step 01:

npm i @muzammil931/pdf-pages-rotation

Step 02:

import {ExtractPdfPages, RotatePdfPage} from '@muzammil931/pdf-pages-rotation'

In this import, ExtractPdfPages function takes argument event and returns back a url which you can use to render your pdf.

The RotatePdfPage1 function takes event as well as an array of numbers as arguments. The event will contain pdf file and array of numbers will contain all those page numbers on which user has clicked on and changed their rotation. So that user can rotate multiple pdf pagess, instead of one at a time.

Step 03:

const pagesAlreadyRotated = [] //stores page numbers of pages that are already rotated.
let pdfFileUrl = '' // stores url of pdf to render pdf in browser
let event = '' // stores event which contains user selected pdf file

  const onFileSelection = async (e) => {
    const { files } = e.target;
    event = e;
         const url = await ExtractPdfPages(e);
         pdfFileUrl = url
  };

This function is an onChange function and whenever the pdf input tag will change, this function will be called and a url will be given, which will contain all pdf pages in a single form.

async function handleRotation (pageNumber){

  const numOfPagesRotated = [...pagesAlreadyRotated, pageNumber]
  const url = await RotatePdfPage(event, numOfPagesRotated);
  pdfFileUrl = url
  <!--
   Logic to show user a rotated page on frontend.
    For example: A logic which will rotate the div in which page is being displayed or to download the updated file. Simply add this:

    // Create a hidden anchor element
    const link = document.createElement('a');
    link.href = pdfFileUrl;
    link.download = 'document.pdf'; // You can specify the filename here
    link.style.display = 'none';

    // Append the anchor element to the document
    document.body.appendChild(link);

    // Simulate a click event to trigger the download
    link.click();

    // Clean up: remove the anchor element and revoke the Blob URL
    document.body.removeChild(link);
    URL.revokeObjectURL(pdfFileUrl);
   -->
}

The handleRotation function will return the url of updated pdf which will contain the pages that user rotated, alongside rest of the pages.

pdf-display-and-rotation's People

Contributors

muzammilirshad avatar

Watchers

 avatar

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.