Coder Social home page Coder Social logo

scriptable-pdfjs's Introduction

scriptable-pdfjs

A PDF text parser for Scriptable App (iOS).

iOS shortcuts PDF conversion is not always the good tool. Due to the "intelligent" column recognition of PDFKit, text output is sometime in total disorder. I needed a parser which always output text in a predictable order, and this tool does that using the pdfjs text extraction.

Building

First install dependencies:

npm install

To create a build:

npm run build

Running from Scriptable

Copy the file dist/scriptable-pdfjs.html into Scriptable Document Folder.

(or you may simply download the latest version from here)

and use the gist scriptable-pdfjs-demo to play.

Running from Shortcuts app

In this example we also expect to find scriptable-pdfjs.html` in Scriptable documents folder

  • Pass a PDF using the sharesheet or an Open File action
  • Save a copy of that file in the Shortcut Folder to allow the next bookmark creation action
  • Add a Scriptable/Create bookmark action
  • Add a Scriptable/Run Inline action containing :
let fm;
try {
  fm = FileManager.iCloud();
} catch(e) {
  fm = FileManager.local();
}
// use the same bookmark name as in the action above
const filePath = fm.bookmarkedPath("ShortcutPDF");

// We execute pdfjs in a WebView
const wv = new WebView();
const htmlFileUrl = fm.joinPath(fm.documentsDirectory(), "scriptable-pdfjs.html");
await fm.downloadFileFromiCloud(htmlFileUrl);
await wv.loadFile(htmlFileUrl);

let javascript = 'pdfjs.getText(';
javascript += '"' + fm.read(filePath).toBase64String() + '"';
//javascript += ', (pageText) => pageText.includes("(Long copy #1)")';
//javascript += ', true';
javascript += ');'

let result = "";
try {
  result = await wv.evaluateJavaScript(javascript, true);
} catch (e) {
  result = "";
}
return result;

For some reasons... (bug in shortcuts or scriptable ?) You cannot convert the PDF to Base64 and pass it as an argument to the script. You have to use the bookmark trick and make the base64 conversion in Scriptable.

For file larger than 2.5 Mo, you can not run this script inline and you have to modify the script to get results by using the clipboard.

scriptable-pdfjs's People

Contributors

flyingeek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.