Skip to content

Incorporate "Open" Package to Refresh PDF Viewers for PDF #11

@davidandrewthompson

Description

@davidandrewthompson

Is your feature request related to a problem? Please describe.
It's tiresome to constantly reload the built pdf into a pdf viewer for each build.

Describe the solution you'd like
It would be great to combine Nodemon and the Open package so that the pdf viewer is automatically updated with the most recently built version of the pdf.

HOW TO SET IT UP:

  1. If you don't have Node.js installed, download the installer from the official website (https://nodejs.org/) and follow the installation instructions.

  2. Open Terminal and install nodemon and open libraries globally with the following command:

npm install -g nodemon open
  1. Create a JavaScript file, for example, refreshPreview.js, and open it with your preferred text editor.

  2. Paste the following JavaScript code into the file:

const open = require('open');
const filePath = process.argv[2];

if (!filePath) {
  console.error('Please provide a file path as an argument.');
  process.exit(1);
}

(async () => {
  console.log(`File ${filePath} changed. Reloading Preview...`);

  // Close the current Preview instance
  await open(filePath, { app: 'Preview', wait: true }).catch(() => {});

  // Reopen the file with Preview
  await open(filePath, { app: 'Preview' });
})();
  1. Save the file and close the text editor.

  2. Now you can run nodemon with the path to the file you want to watch as an argument. Open Terminal and navigate to the folder containing the refreshPreview.js script, then run:

nodemon --watch /path/to/your/file.pdf --exec "node refreshPreview.js /path/to/your/file.pdf"

With this command, nodemon will watch the specified file for changes, and the Preview window will automatically refresh the file whenever it's updated, even if it's in the background. To stop the script, press Control + C in Terminal.

Please note that this solution uses Node.js and won't work directly in a web browser. If you're looking for a solution that works within a web browser, you may consider building a web-based PDF or image viewer using JavaScript libraries such as PDF.js or Viewer.js.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions