Jump to content

Digital signature of pdfs with AutoFirma tool from command line


 Share

Recommended Posts

Autofirma is an open source tool quite used in Spain for signing files:

http://firmaelectronica.gob.es/Home/Descargas.html

I was trying to adapt code found here: https://github.com/ctt-gob-es/clienteafirma/issues/20

In the example below i get the signed file, but don't know how to personalize the signature text using AutoFirma fields.

#cs ----------------------------------------------------------------------------
https://github.com/ctt-gob-es/clienteafirma/issues/20
AutoFirma: Firmar desde linea de comandos

Author: Joaquín Ferrero. 2021.03.06
joaquinferrero, Mar 6 '21

   Lista de campos para AutoFirma:
   $$SUBJECTCN$$ Nombre común (CN, Common Name) del titular del certificado de firma.
   $$ISSUERCN$$ Nombre común (CN, Common Name) del emisor del certificado de firma.
   $$CERTSERIAL$$ Número de serie del certificado de firma.
   $$SIGNDATE=PATRÓN$$ Fecha de la firma.
   $$GIVENNAME$$ Nombre del titular (G, Given Name) (podría estar vacío).
   $$SURNAME$$ Apellidos del titular (SN, Surname) (podría estar vacío).
   $$ORGANIZATION$$ Organización (O, Organization) (podría estar vacío).
   $$REASON$$ Razón por la que se firma el PDF. (podría estar vacío).
   $$LOCATION$$ Ciudad en la que se firma el PDF. (podría estar vacío).
   $$CONTACT$$ Información de contacto del firmante del PDF. (podría estar vacío).

   #===== Formato de fecha: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html =====
   ;~ Global $FECHA = "EEEE d 'de' MMMM 'de' yyyy, HH:mm:ss"

   #===== Texto que queremos que aparezca en la firma =====
   ;~ Global $LAYER2TEXT = 'Firmado el $$SIGNDATE=' & $FECHA &'$$' _
   ;~    & ' Firma digital de $$GIVENNAME$$ $$SURNAME$$' _
   ;~    & ' Certificado emitido por FNMT-RCM'
#ce ----------------------------------------------------------------------------

#===== CONFIG =====
Global $NIF = "99999018D"
Global $sFileCert = @DesktopDir & "\Ciudadano_autenticación_activo.pfx"
;Global $sFileCert = @DesktopDir & "\myCertificate.p12"
Global $sPwdCert = "369258"
Global $sInputFile = @DesktopDir & "\test.pdf"
Global $sFileAutoFirma = "C:\Archivos de programa\AutoFirma\AutoFirma\autofirma.exe"

Global $PositionOnPageLowerLeftX = '350'
Global $PositionOnPageLowerLeftY = '92'
Global $PositionOnPageUpperRightX = '540'
Global $PositionOnPageUpperRightY = '174'
Global $l2FontColor = 'black'
Global $l2FontSize = '9'
Global $l2FontFamily = '1'
Global $l2FontStyle = '0'
Global $signaturaPage = '1'

#===== Configuración para AutoFirma =====
;Ok: showing predefined text
Global $sConfig = 'signaturePositionOnPageLowerLeftX=' & $PositionOnPageLowerLeftX & '\n' _
   & 'signaturePositionOnPageLowerLeftY=' & $PositionOnPageLowerLeftY & '\n' _
   & 'signaturePositionOnPageUpperRightX=' & $PositionOnPageUpperRightX & '\n' _
   & 'signaturePositionOnPageUpperRightY=' & $PositionOnPageUpperRightY & '\n' _
   & 'layer2FontColor=' & $l2FontColor & '\n' _
   & 'layer2FontSize=' & $l2FontSize & '\n' _
   & 'layer2FontFamily=' & $l2FontFamily & '\n' _
   & 'layer2FontStyle=' & $l2FontStyle & '\n' _
   & 'signaturePage=' & $signaturaPage

;~ Global $sConfig = 'signaturePositionOnPageLowerLeftX=' & $PositionOnPageLowerLeftX & '\n' _
;~    & 'signaturePositionOnPageLowerLeftY=' & $PositionOnPageLowerLeftY & '\n' _
;~    & 'signaturePositionOnPageUpperRightX=' & $PositionOnPageUpperRightX & '\n' _
;~    & 'signaturePositionOnPageUpperRightY=' & $PositionOnPageUpperRightY & '\n' _
;~    & 'layer2FontColor=' & $l2FontColor & '\n' _
;~    & 'layer2FontSize=' & $l2FontSize & '\n' _
;~    & 'layer2FontFamily=' & $l2FontFamily & '\n' _
;~    & 'layer2FontStyle=' & $l2FontStyle & '\n' _
;~    & 'signaturePage=' & $signaturaPage & '\n' _
;~    & 'layer2Text=Firmado por $$GIVENNAME$$ $$SURNAME$$' _ ;<<<<< Fails converting fields to values

Global $iReturn = RunWait('"' & $sFileAutoFirma _
   & '" sign -i "' & $sInputFile & '"' _
   & ' -o "' & StringReplace($sInputFile, ".pdf", "_signed.pdf") & '"' _
   & ' -format pades' _
   & ' -store pkcs12:"' & $sFileCert & '"' _
   & ' -password ' & $sPwdCert _
   & ' -filter subject.contains:' & $NIF _
   & ' -config "' & $sConfig & '"' _
   , "", @SW_HIDE)

ConsoleWrite($iReturn & @CRLF)

Using: AutoFirma 1.4.3.0 (file from Universidad de Valladolid) and Windows XP.

Attached file: a testing certificate file from https://www.dnielectronico.es/PortalDNIe/PRF1_Cons02.action?pag=REF_1116&id_menu=68

Ciudadano_autenticación_activo.pfx

Edited by robertocm
Link to comment
Share on other sites

Solved: it seems related with the version of AutoFirma.

See page 161: Manual del integrador del MiniApplet v1.6 del Cliente @firma y la compatibilidad de sus despliegues con AutoFirma

Code below working ok with AutoFirma 1.6.5 (Windows 7) :

Global $sFileAutoFirma = "C:\Program Files\AutoFirma\AutoFirma\AutoFirma.exe"
;or
;Global $sFileAutoFirma = "C:\Program Files\AutoFirma\AutoFirma\AutoFirmaCommandLine.exe"

 

Global $sConfig = 'signaturePositionOnPageLowerLeftX=' & $PositionOnPageLowerLeftX & '\n' _
   & 'signaturePositionOnPageLowerLeftY=' & $PositionOnPageLowerLeftY & '\n' _
   & 'signaturePositionOnPageUpperRightX=' & $PositionOnPageUpperRightX & '\n' _
   & 'signaturePositionOnPageUpperRightY=' & $PositionOnPageUpperRightY & '\n' _
   & 'layer2FontColor=' & $l2FontColor & '\n' _
   & 'layer2FontSize=' & $l2FontSize & '\n' _
   & 'layer2FontFamily=' & $l2FontFamily & '\n' _
   & 'layer2FontStyle=' & $l2FontStyle & '\n' _
   & 'signaturePage=' & $signaturaPage & '\n' _
   & 'layer2Text=Firmado por $$SUBJECTCN$$ el día $$SIGNDATE=dd/MM/yyyy$$ Certificado $$ISSUERCN$$' ;<<<<< OK

Another test: all these fields returning data:

& 'layer2Text=Firmado por $$LOCATION$$ $$ORGANIZATION$$ $$GIVENNAME$$ $$SURNAME$$. $$SUBJECTCN$$ el día $$SIGNDATE=dd/MM/yyyy$$ Certificado $$ISSUERCN$$' ;<<<<< OK

 

Edited by robertocm
Link to comment
Share on other sites

  • 11 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...