Adjusting the viewer.js script

In order to view my pdf files using Mozilla's pdfjs plugin, I currently pass a query parameter to viewer.html like this:

http://localhost/MyProject/viewer.html/?file=file.pdf

Although this method works fine for me, I have a unique requirement for my project. I want to implement a tab-like feature on a single page, where each tab contains a different pdf file.

My plan is to refactor the code in the viewer.js file and turn it into a large function that can be used as a constructor to render individual pdf files. For example:

var firstPdf = new paintPdf({file: 'myfile.pdf'});

However, I have decided to tackle this modification later, once I successfully integrate pdfjs's viewer functionality into my project.

A brief overview of my project:

  • This project is a single page application.
  • All templates are stored within a single file under an object named templates.

To achieve my goal, I started by copying all the HTML content inside the body tags of viewer.html and adding it as a new property to the templates object. Then, I copied the required files from the example to my project folder and loaded them dynamically. The necessary files include:

  • pdf.js
  • pdf.worker.js
  • viewer.js
  • l10n.js
  • viewer.css - (this file is not loaded dynamically)

After loading these files, I attempted to render the template from viewer.html using lodash. However, I am unable to see the rendered pdf in my project. I suspect this could be due to the dynamic nature of the process (although everything appears to be rendering in sequence).

As a side note, I have placed a default pdf file named

compressed.tracemonkey-pldi-09.pdf
next to the index.html file. What might I be overlooking?

I have not encountered any errors on Firefox or Chrome.

Please advise if I am approaching this task incorrectly and suggest ways to steer me in the right direction.

Answer №1

When making modifications to viewer.js, there are some key points to keep in mind.

  • It is advisable to create your own version of viewer.js rather than altering the provided demo file, as it is primarily for demonstration purposes.
  • To develop your own viewer.js file, you can explore each JavaScript file on this link.

If you only need to make minor changes to the existing demo viewer.js, then

  • Specify the exact path for pdf.worker.js file within your viewer.js.
  • This file initiates pdf rendering on the DomContentLoaded event. If you intend to render the pdf dynamically at a later point, you may want to disable this event registration and call the following function when needed:

    webViewerLoad();
    

Hopefully, these tips will be beneficial to someone.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing the row delete function in MUI DataGrid using JavaScript

My grid has a delete icon button in each row, but it's not deleting the row. Can someone please help me with this issue? I'm working on a todo app where each row should have its own delete button. I'm struggling to connect the deleteTodo co ...

Tips for grouping radio buttons that span multiple rows within an ag-grid

I am currently working on a grid layout that consists of multiple rows, with each row containing a radio button as illustrated in the snapshot below: https://i.sstatic.net/O8i0r.png Here is the code snippet for the column definition of the radio button: ...

The default theme has not been specified in Tailwind CSS

During my recent project, I utilized React, Next.js in combination with Tailwind CSS. In this project, I delved into styling customization by implementing react-slick for a specialized slider. To achieve the desired aesthetic, I made modifications to the r ...

Regular expression that can be used to extract information from a text file by filtering and returning only

When I open a text file, it contains several lines like the examples below. surname australia enter name j.jonhs enter name j.cause enter name f.london enter I am seeking assistance to modify the code snippet below so that it captures the first line m ...

Steps for wrapping a class with a higher order component

Is it feasible to encapsulate a class component within a higher order component (HOC) that is also a class? import React, { Component } from "react"; import { View } from "react-native"; import { Toast } from "react-native-easy-toast"; const withToast = ...

Retrieve data via AJAX using a combination of JavaScript and ASP

Can someone help me figure out how to retrieve the value of value1 on my server-side ASP using Javascript? I am using this Ajax code but unsure of how to do it. In my serverside code, I would like to have something like <% var newdata = value1 (which ...

magnific popup: trigger the display by clicking on an element other than the image

A recent request from the client calls for the image caption to cover the thumbnail fully when hovered over. To meet this requirement, I now need to enable clicking on the caption to open Magnific Popup rather than using the <a> tag. Here is what I h ...

Tips on ensuring the <script> section of a Vuejs single file component loads prior to the <template> section

After posing my query here, I have come to the realization that the root of my problem might be due to the template loading before the script. This causes an error when it encounters an undefined variable, halting the script execution. The issue could pote ...

Why isn't my JavaScript AJAX PHP if statement functioning properly?

I have been struggling with this issue for more than two hours now and cannot seem to find a logical solution. When I remove the If statement highlighted by --> this arrow, the alert() function works perfectly fine. It triggers when I simply use if(true ...

Display images in a list with a gradual fade effect as they load in Vue.js

In my Vue project, I am looking to display images one at a time with a fading effect. I have added a transition group with a fade in effect and a function that adds each image with a small delay. However, I am facing an issue where all the images show up ...

Using a JavaScript command, connect a function from one file to another file

I attempted to import a function because I want to click on <il> servies</il> and scroll to the services section on the home page. However, I simply want to click on the li element in the navbar and scroll to the service section on the home pag ...

Is the .html page cached and accessible offline if not included in the service-worker.js file?

During the development of my PWA, I encountered an unexpected behavior with caching. I included a test .html page for testing purposes that was not supposed to be cached in the sw.js folder. Additionally, I added some external links for testing. However, w ...

Having trouble accessing the input class with jQuery

When I click the "Reset To Default Settings" button in my form, I want to clear the default colors in my 4 color pickers. Each of them has an input type text field with the class anyicon-form-colorpicker. To achieve this, I locate the <a> tag and use ...

Can Cesium "separate" intersecting polylines?

Currently, I am utilizing Cesium and aiming to visually display multiple polylines between two entities. Specifically, I want a green polyline from entity A to entity B, as well as a blue polyline from entity A to entity B. My intention is for these lines ...

How to pass the Exact property in React MaterialUI Button with RouterLink Component?

I came across this code snippet: <Button activeClassName={classes.active} className={classes.button} component={RouterLink} exact={true} to="/app/certificates" > Certificates </Button> <Button activeClassName={classe ...

Using ReactJS to create cross-browser inline styling with flexbox

I'm currently working on a React web application that relies heavily on inline styling. My goal is to ensure compatibility with the latest versions of major browsers (Safari, Chrome, Firefox, IE) while utilizing flexbox for layout. The issue I encou ...

The error message "Property 'push' of undefined in AngularJS" occurs when the push method is being called

I'm currently working on developing a basic phonebook web application to enhance my knowledge of Angular, but I've encountered an issue with this error message - "Cannot read property 'push' of undefined". Can anyone help me identify th ...

The middleware function encountered an undefined value in req.body

I've been attempting to validate my data retrieved from my express endpoint using yup. Despite watching numerous tutorials, I am encountering a particular error that has me stuck. Every time I try to validate the data in my middleware, it consistentl ...

Having trouble retrieving text from an HTML form in Node.js using express, as the req.body object is coming up

Having trouble extracting text from an HTML form to build a basic text to speech functionality. Despite using an express server, the req.body is consistently empty when attempting to fetch the text. I've experimented with body-parser and adjusted the ...

The error message "404 Not found" is returned when attempting to retrieve a Google spreadsheet

I have exhausted all of my options on the web trying to retrieve my Google spreadsheet as a json file. $.getJSON() However, I am constantly receiving a 404 Not Found error. When I publish to the web, the URL I get is: https://docs.google.com/spreadsheets ...