Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@lillallol/outline-pdf
Advanced tools
Readme
NOTICE: If you have outlined your pdf using a version from 2.0.0 to 3.0.3, then if you edit your pdf it will get corrupted due to a bug introduced by my module. From version 3.0.4 this bug has been resolved.
npm install @lillallol/outline-pdf
Take a look at outline-pdf-cjs if you are using node.
Adds outline to pdf that do not have one. As of right now, it is the only module in the npm registry that has the following characteristics :
Testing code coverage is more than 90%.
import * as fs from "fs";
import * as path from "path";
import * as pdfLib from "pdf-lib";
import { outlinePdfFactory } from ".";
jest.setTimeout(60 * 10e3);
describe(outlinePdfFactory.name, () => {
it("adds the provided outline to the loaded pdf", async () => {
const outlinePdf = outlinePdfFactory(pdfLib);
const absolutePathToPdfToOutline = path.resolve(__dirname, "test.pdf");
const absolutePathToSaveOutlinedPdf = path.resolve(__dirname, "test.outline.pdf");
try {
fs.unlinkSync(absolutePathToSaveOutlinedPdf);
} catch (e) {
//file does not exist so no problem
}
const pdf = fs.readFileSync(absolutePathToPdfToOutline, { encoding: "base64" });
const outline: string = `
1||Some random title 1
2|-|Some random title 2
-3|--|Some random title 3
4|---|Some random title 4
5|---|Some random title 5
6|-|Some random title 6
7||Some random title 7
`;
const outlinedPdf = await outlinePdf({ outline, pdf }).then((pdfDocument) => pdfDocument.save());
fs.writeFileSync(absolutePathToSaveOutlinedPdf, outlinedPdf);
});
});
export declare const outlinePdfFactory: IOutlinePdfFactory;
# IOutlinePdfFactory
export declare type IOutlinePdfFactory = (pdfLib: pdfLib) => IOutlinePdf;
# pdfLib
/** * @description * Just use: * * ```ts * import * as pdfLib from "pdf-lib"; * ``` */ export declare type pdfLib = { PDFDict: typeof PDFDict; PDFName: typeof PDFName; PDFNumber: typeof PDFNumber; PDFRef: typeof PDFRef; PDFArray: typeof PDFArray; PDFNull: typeof PDFNull; PDFDocument: typeof PDFDocument; PDFPageLeaf: typeof PDFPageLeaf; PDFHexString: typeof PDFHexString; };
# PDFDict
import type { PDFDict } from "pdf-lib";
# PDFName
import type { PDFName } from "pdf-lib";
# PDFNumber
import type { PDFNumber } from "pdf-lib";
# PDFRef
import type { PDFRef } from "pdf-lib";
# PDFArray
import type { PDFArray } from "pdf-lib";
# PDFNull
import type { PDFNull } from "pdf-lib";
# PDFDocument
import type { PDFDocument } from "pdf-lib";
# PDFPageLeaf
import type { PDFPageLeaf } from "pdf-lib";
# PDFHexString
import type { PDFHexString } from "pdf-lib";
# IOutlinePdf
export declare type IOutlinePdf = (_: { /** * @description * The pdf to outline. * * I you provide the pdf as a string, then make sure it is of base 64. */ pdf: string | Uint8Array | ArrayBuffer | PDFDocument; /** * @description * String representation of the outline. * * Example: * * ```ts * ` * 1||some title 1 * 12|-|some title 2 * -30|--|some title 3 * 34|---|some title 4 * 35|---|some title 5 * 60|--|some title 6 * 67|-|some title 7 * 80||some title 8 * ` * ``` * * where the: * * * first column is the page number, and if it negative it means that this * part of the outline is collapsed * * second column is the outline depth * * third column is the outline title */ outline: string; }) => Promise<PDFDocument>;
I had to use this module in one of my projects: scrap-it. I could not find one that satisfied my needs, so I decided to create my own.
The following comments : 1 , 2 , contributed tremendously in the creation of this module.
pdf-lib
to use this module.No pdf-lib
code is hard coded in this module. When someone wants to use this module and also use pdf-lib
, to create a new module, there will be no pdf-lib
code repeated in the generated bundle.
I am open to suggestions/pull request to improve this program.
You will find the following commands useful:
Clones the github repository of this project:
git clone https://github.com/lillallol/outline-pdf
Installs the node modules (nothing will work without them):
npm install
Tests the source code:
npm run test
Lints the source folder using typescript and eslint:
npm run lint
Builds the typescript code from the ./src
folder to javascript code in ./dist
:
npm run build-ts
Injects in place the generated toc and imported files to README.md
:
npm run build-md
Checks the project for spelling mistakes:
npm run spell-check
Take a look at the related configuration ./cspell.json
.
Checks ./src
for dead typescript files:
npm run dead-files
Take a look at the related configuration ./unimportedrc.json
.
Logs in terminal which dependencies
and devDependencies
have a new version published in npm:
npm run check-updates
Updates the dependencies
and devDependencies
to their latest version:
npm run update
Formats all .ts
files of the ./src
folder:
npm run format
I want to:
using pdf-lib
.
Finally I want to create the following tests:
Breaking changes
outlinePdf
has been simplified.Other
README.md
browser example, with the test file of the entry point function.README.md
.Bug fixes
@lillallol/outline-pdf-data-structure
without .js
extension.Other
CHANGELOG.md
.bug fixes
bug fixes
breaking changes
outlinePdfCjs
is now on its own package: @lillallol/outline-pdf-cjs
.bug fixes
README.md
.README.md
.Breaking changes
outlinePdf
is renamed to outlinePdfCjsFactory
. You now have to inject pdf-lib
in the factory.outlinePdfFactory
. It can outline pdf in both browser and nodejs. You need to inject pdf-lib
in the factory.Other
README.md
.MIT
FAQs
High level API to add outline to a pdf without any dependency on other programming languages. Works in both browser and node.
The npm package @lillallol/outline-pdf receives a total of 2,951 weekly downloads. As such, @lillallol/outline-pdf popularity was classified as popular.
We found that @lillallol/outline-pdf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.