DEV Community

Derek
Derek

Posted on

Build Online Tools to Measure PDFs

When working with PDF files online, having the ability to measure dimensions and content directly within the browser can significantly enhance productivity. 

This guide will walk you through the simple process of building an online PDF measurement tool using ComPDFKit's Web SDK.

Prerequisites

To get started, you'll need to check whether you have met these requirements by visiting our "Web SDK guides" page:

  • The latest stable version of Node.js.
  • A package manager compatible with npm.
  • Apply the License Key

How to Get a 30-day Free License

ComPDFKit offers a 30-day free license key for everyone to test their project. And you can get it with the following steps:

  • Visit ComPDFKit's Pricing page
  • Then input the required information in the form
  • Finally, Click the "Get Your Free Trial License" button and check your email inbox after waiting a moment, the license key will be there.

Image description

Integrate Measurement PDF into Web Project - Full Guide

Step 1: Install the ComPDFKit for Web package from npm into your Vanilla JS project

npm i @compdfkit_pdf_sdk/webviewer --save
Enter fullscreen mode Exit fullscreen mode

Then, Add the "webviewer" folder which contains the required static resource files to run the ComPDFKit Web demo, to your project's public resource folder.

cp ./node_modules/@compdfkit_pdf_sdk/webviewer/webviewer.global.js ./
cp -r ./node_modules/@compdfkit_pdf_sdk/webviewer/dist ./webviewer
Enter fullscreen mode Exit fullscreen mode

Step 2: Create index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ComPDFKit Web Viewer</title>
</head>
<!-- Import WebViewer as a script tag -->
<script src='./webviewer.global.js'></script>
<body>
  <div id="app" style="width: 100%; height: 100vh;"></div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Note: when you use the Web SDK, you need to use the parameter "path" to tell it where the static resources are located. If you don't do this step, then they are relative to the current path

Step 3: Add a script tag and initialize ComPDFKitViewer for Web using Vanilla JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ComPDFKit Web Viewer</title>
</head>
<!-- Import WebViewer as a script tag -->
<script src='./webviewer.global.js'></script>
<body>
  <div id="app" style="width: 100%; height: 100vh;"></div>
​
  <script>
    let docViewer = null;
​
    ComPDFKitViewer.init({
      path: '/',
      pdfUrl: '/webviewer/example/developer_guide_web.pdf',
      license: '<Input your license here>'
    }, document.getElementById('app')).then((core) => {
      docViewer = core.docViewer;
​
      docViewer.addEvent('documentloaded', async () => {
        console.log('document loaded');
      })
    })
  </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 4: In order to display on the local host, we need to set up the server environment in this step.

npm install -g http-server
Enter fullscreen mode Exit fullscreen mode

Step 5: Open http://localhost:8080 on your browser. Then the PDF file will be displayed and you can switch to the PDF Measurement tool to measure your PDF file.

http-server -a localhost -p 8080
Enter fullscreen mode Exit fullscreen mode

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay