Improve signature sizing

Fixes #13.

- Improve README.md to clarify signature anchor point.
- Change -x and -y
  - Require % unit
  - Support specifying coordinate relative both edges.
- Add -r / --resize
- Change -W and -H
  - Add support for units
  - Change default to 50%
Dieser Commit ist enthalten in:
Axel Svensson 2024-09-08 04:03:48 +02:00
Ursprung bcc37b0929
Commit 9c7ba6da50
4 geänderte Dateien mit 41 neuen und 8 gelöschten Zeilen

BIN
README-example-signature.gif Normale Datei

Binäre Datei nicht angezeigt.

Nachher

Breite:  |  Höhe:  |  Größe: 22 KiB

Datei anzeigen

Vorher

Breite:  |  Höhe:  |  Größe: 342 KiB

Nachher

Breite:  |  Höhe:  |  Größe: 342 KiB

Datei anzeigen

@ -5,7 +5,7 @@
A tool to sign PDF files, with Linux support.
We are here referring to the visible, non-cryptographic squiggles.
![](README-example.gif)
![](README-example-use.gif)
## How
@ -16,6 +16,11 @@ The recommended way is:
* Use an application of your choice to sign it.
You can for example use Okular's Freehand Line, or transfer it to your smartphone and use Adobe Acrobat Reader.
Keep in mind that it's the center of this mini-page that will be used for positioning the signature.
<img src="README-example-signature.gif" width="250"/>
It's a good idea to write your signature on an imagined line through the center of the mini-page.
That way, it can be positioned correctly by clicking on the signature line.
* Put the signed file in your signature directory.
The signature directory is `$PDF_SIGNATURE_DIR`, `$XDG_CONFIG_HOME/pdf_signatures`, `$HOME/.config/pdf_signatures` or `$HOME/.pdf_signatures/`; the first one that exists. Use `pdf-sign -h` to confirm which one will be used on your system.

Datei anzeigen

@ -113,8 +113,18 @@ def main(args):
return fileName
signaturePath._cache={}
signatureSize=Cell(lambda: pdfGetSize(signaturePath()))
signaturePositionX=Cell(args.x_coordinate)
signaturePositionY=Cell(args.y_coordinate)
try:
xm=m("^(L\\+|L-|R\\+|R-|-|)([0-9.]+)%$", args.x_coordinate)
default_x=(lambda x:{'L+':x,'L-':-x,'R+':1+x,'R-':1-x,'-':-x,'':x}[xm[1]])(float(xm[2])/100)
except:
die('Invalid -x option')
try:
ym=m("^(T\\+|T-|B\\+|B-|-|)([0-9.]+)%$", args.y_coordinate)
default_y=(lambda y:{'T+':y,'T-':-y,'B+':1+y,'B-':1-y,'-':-y,'':y}[ym[1]])(float(ym[2])/100)
except:
die('Invalid -y option')
signaturePositionX=Cell(default_x)
signaturePositionY=Cell(default_y)
signatureScale=Cell(0)
def translatablePDF(path):
cache = translatablePDF._cache
@ -162,11 +172,28 @@ def main(args):
die(f"The PDF at {path} is unusable as a signature. Reason unknown.")
return cache[path]
translatablePDF._cache={}
@Cell
def defaultResizeFactor():
(pageWidth, pageHeight)=pageSize()
try:
wm=m("^([0-9.]+)(pts|pt|in|cm|mm|%)$", args.width)
maxSignatureWidth=int(float(wm[1])*({'pts':1,'pt':1,'in':72,'cm':28.3,'mm':2.83,'%':pageWidth/100}[wm[2]]))
except:
die('Invalid -W option')
try:
hm=m("^([0-9.]+)(pts|pt|in|cm|mm|%)$", args.height)
maxSignatureHeight=int(float(hm[1])*({'pts':1,'pt':1,'in':72,'cm':28.3,'mm':2.83,'%':pageHeight/100}[hm[2]]))
except:
die('Invalid -H option')
(signatureWidth, signatureHeight)=signatureSize()
return min(args.resize_factor,
maxSignatureWidth / signatureWidth,
maxSignatureHeight / signatureHeight)
@VolatileCell
def signaturePositionedPDF():
(w, h)=pageSize()
(sw, sh)=signatureSize()
resize=1.1**signatureScale()*min(args.width*w/sw, args.height*h/sh)
resize=1.1**signatureScale()*defaultResizeFactor()
dx=w*signaturePositionX()/resize - sw/2
dy=h*(1-signaturePositionY())/resize - sh/2
outFile=intmp('signature-positioned.pdf')
@ -749,10 +776,11 @@ parser = argparse.ArgumentParser(description='Sign a PDF file, with a non-crypto
parser.add_argument('input', metavar='input.pdf', type=str, help='Input PDF file.')
parser.add_argument('-p', '--page', type=int, default=-1, help='The page to sign, negative for counting from the end. (default: -1)')
parser.add_argument('-s', '--signature', type=str, help=getSignatureHelp())
parser.add_argument('-x', '--x-coordinate', type=float, default=0.5, help='Horizontal coordinate of signature center, in page width units. (default: 0.5)')
parser.add_argument('-y', '--y-coordinate', type=float, default=0.75, help='Vertical coordinate of signature center, in page height units. (default: 0.75)')
parser.add_argument('-W', '--width', type=float, default=0.28, help='Width of box to fit signature to, in page width units. (default: 0.28)')
parser.add_argument('-H', '--height', type=float, default=0.11, help='Height of box to fit signature to, in page height units. (default: 0.11)')
parser.add_argument('-x', '--x-coordinate', type=str, default="50%", help='Horizontal coordinate of signature center. Requires unit %% (meaning percent of page width). May be preceded by L+ (default), L-, R+ or R- to give coordinate relative left of right edge of the page. For example, 25%% means a quarter page width from left edge, R-3%% means 3%% of page width from right edge. (default: 50%%)')
parser.add_argument('-y', '--y-coordinate', type=str, default="B-25%", help='Vertical coordinate of signature center. Requires unit %% (meaning percent of page height). May be preceded by T+ (default), T-, B+ or B- to give coordinate relative top or bottom edge of the page. For example, 3%% means 3%% of page height from top edge, B-10%% means 10%% of page height from bottom edge. (default: B-25%%)')
parser.add_argument('-r', '--resize-factor', type=float, default=1.0, help='Resize signature by this factor. If this would make the signature larger than allowed by -W or -H, then instead use the maximum allowed size.')
parser.add_argument('-W', '--width', type=str, default="50%", help='Maximum width of signature. Supports units pts, in, cm, mm and %% (meaning percent of page width). (default: 50%%)')
parser.add_argument('-H', '--height', type=str, default="50%", help='Maximum height of signature. Supports units pts, in, cm, mm and %% (meaning percent of page height). (default: 50%%)')
parser.add_argument('-o', '--output', type=str, help='Output file. (default: Add ".signed" before the extension)')
parser.add_argument('-b', '--batch', action=argparse.BooleanOptionalAction, default=False, help='Batch mode: do not show GUI. (default: False)')
parser.add_argument('-f', '--flatten', action=argparse.BooleanOptionalAction, default=True, help='Flatten before signing, preventing subsequent changes in PDF forms. (default: True)')