Web PKI - Local PDF Signature

by dshilkret

HTML

<script type="text/javascript" src="https://cdn.lacunasoftware.com/libs/web-pki/lacuna-web-pki-2.13.0.min.js" integrity="sha256-nVy9sLakeQ03Xl+jOjBdjdfnZ9UVwwqNgAYszhxi4VQ=" crossorigin="anonymous"></script>

<h2>Web PKI - Local PDF Signature</h2>
<h4>Serial signature sample with PDF passed from JavasSript and signed PDF returned to JavasSript</h4>
<p>Select a certificate:
  <br/>
  <select id="certificateSelect"></select>
</p>
<p>
  <button id="refreshButton" type="button">Refresh Certificates</button>
</p>
<p>
  <button id="signPadesButton" type="button">Sign PDF</button>
  <button id="signPadesAgainButton" type="button">Sign PDF Again</button>
</p>
<p>Signed PDF content:</p>
<textarea id="contentResult" rows="8" cols="100"></textarea>
<p>
  <a id="downloadPdfLink" href='#' download="signed.pdf">Download Signed PDF</a>
</p>
<h4>Logs:</h4>
<div id="logPanel"></div>

CSS

@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700);
body {
  font-family: 'Open Sans', sans-serif;
  font-style: normal;
  font-weight: 400;
  font-size: 14px;
}

#logPanel {
  font-family: monospace;
}

JavaScript

// ------------------------------------------------------------------------------------------
// LacunaWebPKI: Local PDF Signature
// Serial signature sample with PDF passed from JavasSript and signed PDF returned to JavasSript

var pdfContentSample = null;
var signedPdfContent = null;

var pki = new LacunaWebPKI('AiEAKi5sYWN1bmFzb2Z0d2FyZS5jb20sanNmaWRkbGUubmV0AAADAFBybwgAANjqpHQf3QgAAWdZXb1mfX0OvXPptndkKubdjvH2Bp2TAY3IW+i5PpTMtBmnQwYw4YMtouSaod41haOgzNYbyveZ6VFpjW1IVAQRlVh1yO66+W2jkNm/ZJLqn3bQ/KgSPnxRN8/6yCykZGw+kGe0mhaahRYXYAakzU8+MaRrTShwmJpr1ko1nPt9o62/9L32Jpaq2TZLQc++C/JzUT0FUX7ycsPD8FfucX/LN4NVh9rFlWN+iPRrZ5jHcYFA1OdG2wFMzBQyF311KNdx3An5xMj1BrHum4ocvNxM7RABh9YClezDlmdZTG7MLe8I/goKoVy/KQ/dN4g06ze+GW5hhU9Hj6CKgUaSriM=');

function start() {
  log('Initializing component');
  pki.init({
    ready: onWebPkiReady,
    defaultFail: onWebPkiError,
    requiredApiVersion: pki.apiVersions.v1_5_2
  });
}

function onWebPkiReady() {
  refreshCertificates();
}

function onWebPkiError(ex) {
  log('Error: ' + ex.userMessage + ' (' + ex.code + ')');
}

// ------------------------------------------

function refreshCertificates() {
  log('Listing certificates');
  pki.listCertificates({
    selectId: 'certificateSelect'
  }).success(function(certs) {
    log(certs.length + ' certificates found');
  });
}

// ------------------------------------------

function signPades() {
  // get selected certificate thumbprint
  var selectedCertThumb = $('#certificateSelect').val();
  if (!selectedCertThumb) {
    alert('Select a certificate!');
    return;
  }

  // clear result box
  $('#contentResult').val('');

  log('Signing PDF content...');

  pki.signPdf({
    content: pdfContentSample, // PDf to sign base64 encoded
    output: {
      mode: pki.outputModes.returnContent // return signed PDF to page
    },
    certificateThumbprint: selectedCertThumb,
    policy: pki.padesPolicies.basic,
    trustArbitrators: [pki.standardTrustArbitrators.pkiBrazil],
   ...