Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Get all texts/images/shapes/SVGs from PDF page #581

Closed
Akxe opened this issue Aug 28, 2020 · 3 comments
Closed

[FEATURE] Get all texts/images/shapes/SVGs from PDF page #581

Akxe opened this issue Aug 28, 2020 · 3 comments

Comments

@Akxe
Copy link

Akxe commented Aug 28, 2020

First of all, this library is awesome. But I am missing one key aspect from this library to be able to use it. Also, I am not the only one. I will try to create some a draft of what I need, along while taking into consideration what others need too.


Get all of a type from a page:

To be able to modify a PDF also means to be able to change existing parts of it.

A PDFNode abstract class will be created. It would be the parent to PDFText, PDFImage, PDFSVG and `PDFShape.

class PDFNode {
  blendMode: BlendMode;
  opacity: number;
  rotate: Rotation;
  x: number;
  y: number;

  // Getter for its local width/height.
  get width(): number;
  get height(): number;
}

PDFPage.getAllTexts(): PDFTextNode[]

This is needed to be able to position one text base on another, Place signature (PNG) into the correct spot etc.

PDFText class is almost a copy of PDFPageDrawTextOptions interface.

class PDFTextNode extends PDFNode {
  color: Color;
  font: PDFFont;
  lineHeight: number;
  maxWidth?: number;
  size: number;
  wordBreaks: string[];
  xSkew: Rotation
  ySkew: Rotation;

  content: string;
}

The PDFPage.getAllTexts(): PDFText[] function will enable consumer, to find and replace text #564, Aligning new text to ones that are already in place. Or just find if the texts are in vector format, or in native text format.

PDFPage.getAllImages(): PDFImageNode[]

class PDFImageNode extends PDFNode {
  height: number;
  width: number;
  xSkew: Rotation
  ySkew: Rotation;
}

PDFPage.getAllShapes(): PDFShapeNode[]

The PDFShapeNode is an abstract parent class to PDFShapeLineNode, PDFShapeElipseNode, PDFShapeRectangleNode (and PDFShapePathNode?).

class PDFShapeNode extends PDFNode {
  color: Color;
}

TODO: Copy from PDFPageDrawXxxOptions.

Square is a special case of PDFShapeRectangle, same goes for circle.

PDFPage.getAllSVGs(): PDFSVGNode[]

class PDFSVGNode extends PDFNode {
  borderColor: Color
  borderDashArray: number[];
  borderDashPhase: number;
  borderLineCap: LineCapStyle
  borderOpacity: number;
  borderWidth: number;
  path: string;
}

Edits to PDFPage

All of the PDFPageDrawXxx functions will now return the appropriate PDFNode.

class PDFPage {
  // ... previously implemented

  // Added Return types
  drawCircle(options: PDFPageDrawCircleOptions): PDFShapeElipseNode;
  drawEllipse(options: PDFPageDrawEllipseOptions): PDFShapeElipseNode;
  drawImage(image: PDFImage, options: PDFPageDrawImageOptions): PDFImageNode;
  drawLine(options: PDFPageDrawLineOptions): PDFShapeLineNode;
  drawRectangle(options: PDFPageDrawRectangleOptions): PDFShapeRectangleNode;
  drawSquare(options: PDFPageDrawSquareOptions): PDFShapeSquareNode;
  drawSVGPath(image: string, options: PDFPageDrawSVGOptions): PDFSVGNode;
  drawText(text: string, options: PDFPageDrawTextOptions): PDFTextNode;

  // New function to appned created Node to document (optional)
  drawNode<N extends PDFNode>(node: N) => N;
}
@Byteschmiede
Copy link

I would love to see this. Is there any method to actual edit existing text elements on a pdf page? Like replace words or something like that?

@se181018
Copy link

se181018 commented Mar 11, 2021

I would also love to see this feature.
I found an abandoned project which, at least I think, capable of also modifying pdfs maybe it coud be of some help.
https://github.com/galkahana/HummusJS

And I think this also correlates to #177

@Hopding
Copy link
Owner

Hopding commented Sep 24, 2021

Added this to the roadmap for tracking: #998.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants