1
1
import React , { useState , useRef , useEffect } from "react" ;
2
- import { PDFDocument , rgb } from "pdf-lib" ;
2
+ import { PDFDocument } from "pdf-lib" ;
3
3
import "../css/./signature.css" ;
4
4
import sign from "../assests/sign3.png" ;
5
5
import stamp from "../assests/stamp2.png" ;
@@ -18,7 +18,9 @@ import ModalHeader from "react-bootstrap/esm/ModalHeader";
18
18
import {
19
19
convertPNGtoJPEG ,
20
20
contractDocument ,
21
- getBase64FromIMG
21
+ getBase64FromIMG ,
22
+ embedDocId ,
23
+ multiSignEmbed
22
24
} from "../utils/Utils" ;
23
25
import { useParams } from "react-router-dom" ;
24
26
import Tour from "reactour" ;
@@ -30,7 +32,7 @@ import Header from "./component/header";
30
32
import RenderPdf from "./component/renderPdf" ;
31
33
import { contractUsers , contactBook , urlValidator } from "../utils/Utils" ;
32
34
import { modalAlign } from "../utils/Utils" ;
33
- import { $ } from "select-dom" ;
35
+
34
36
//For signYourself inProgress section signer can add sign and complete doc sign.
35
37
function SignYourSelf ( ) {
36
38
const [ pdfDetails , setPdfDetails ] = useState ( [ ] ) ;
@@ -422,24 +424,7 @@ function SignYourSelf() {
422
424
//checking if signature is only one then send image url in jpeg formate to server
423
425
if ( xyPostion . length === 1 && xyPostion [ 0 ] . pos . length === 1 ) {
424
426
//embed document's object id to all pages in pdf document
425
- for ( let i = 0 ; i < allPages ; i ++ ) {
426
- const font = await pdfDoc . embedFont ( "Helvetica" ) ;
427
-
428
- const fontSize = 10 ;
429
- const textContent =
430
- documentId && `OpenSign™ DocumentId: ${ documentId } ` ;
431
-
432
- const pages = pdfDoc . getPages ( ) ;
433
- const page = pages [ i ] ;
434
-
435
- page . drawText ( textContent , {
436
- x : 10 ,
437
- y : page . getHeight ( ) - 10 ,
438
- size : fontSize ,
439
- font,
440
- color : rgb ( 0.5 , 0.5 , 0.5 )
441
- } ) ;
442
- }
427
+ await embedDocId ( pdfDoc , documentId , allPages ) ;
443
428
const pdfBase64 = await pdfDoc . saveAsBase64 ( {
444
429
useObjectStreams : false
445
430
} ) ;
@@ -475,85 +460,15 @@ function SignYourSelf() {
475
460
}
476
461
//else if signature is more than one then embed all sign with the use of pdf-lib
477
462
else if ( xyPostion . length > 0 && xyPostion [ 0 ] . pos . length > 0 ) {
478
- for ( let i = 0 ; i < allPages ; i ++ ) {
479
- const font = await pdfDoc . embedFont ( "Helvetica" ) ;
480
-
481
- const fontSize = 10 ;
482
- const textContent =
483
- documentId && `OpenSign™ DocumentId: ${ documentId } ` ;
484
-
485
- const pages = pdfDoc . getPages ( ) ;
486
- const page = pages [ i ] ;
487
-
488
- page . drawText ( textContent , {
489
- x : 10 ,
490
- y : page . getHeight ( ) - 10 ,
491
- size : fontSize ,
492
- font,
493
- color : rgb ( 0.5 , 0.5 , 0.5 )
494
- } ) ;
495
- }
496
- for ( let i = 0 ; i < pngUrl . length ; i ++ ) {
497
- const pageNo = pngUrl [ i ] . pageNumber ;
498
-
499
- const imgUrlList = pngUrl [ i ] . pos ;
500
- const pages = pdfDoc . getPages ( ) ;
501
- const page = pages [ pageNo - 1 ] ;
502
-
503
- const images = await Promise . all (
504
- imgUrlList . map ( async ( url ) => {
505
- let signUrl = url . SignUrl ;
506
-
507
- if ( url . ImageType === "image/png" ) {
508
- //function for convert signature png base64 url to jpeg base64
509
- const newUrl = await convertPNGtoJPEG ( signUrl ) ;
510
- signUrl = newUrl ;
511
- }
512
- const checkUrl = urlValidator ( signUrl ) ;
513
- if ( checkUrl ) {
514
- signUrl = signUrl + "?get" ;
515
- }
516
- const res = await fetch ( signUrl ) ;
517
-
518
- return res . arrayBuffer ( ) ;
519
- } )
520
- ) ;
521
- images . forEach ( async ( imgData , id ) => {
522
- let img = await pdfDoc . embedJpg ( imgData ) ;
523
- const imgHeight = imgUrlList [ id ] . Height
524
- ? imgUrlList [ id ] . Height
525
- : 60 ;
526
- const imgWidth = imgUrlList [ id ] . Width ? imgUrlList [ id ] . Width : 150 ;
527
- const isMobile = window . innerWidth < 767 ;
528
- const newWidth = window . innerWidth ;
529
- const scale = isMobile ? pdfOriginalWidth / newWidth : 1 ;
530
-
531
- const posY = ( ) => {
532
- if ( isMobile ) {
533
- if ( id === 0 ) {
534
- return (
535
- page . getHeight ( ) -
536
- imgUrlList [ id ] . yPosition * scale -
537
- imgHeight
538
- ) ;
539
- } else if ( id > 0 ) {
540
- return page . getHeight ( ) - imgUrlList [ id ] . yPosition * scale ;
541
- }
542
- } else {
543
- return page . getHeight ( ) - imgUrlList [ id ] . yPosition - imgHeight ;
544
- }
545
- } ;
546
- page . drawImage ( img , {
547
- x : isMobile
548
- ? imgUrlList [ id ] . xPosition * scale + imgWidth / 2
549
- : imgUrlList [ id ] . xPosition ,
550
- y : posY ( ) ,
551
- width : imgWidth ,
552
- height : imgHeight
553
- } ) ;
554
- } ) ;
555
- }
556
- const pdfBytes = await pdfDoc . saveAsBase64 ( { useObjectStreams : false } ) ;
463
+ //embed document's object id to all pages in pdf document
464
+ await embedDocId ( pdfDoc , documentId , allPages ) ;
465
+ //embed multi signature in pdf
466
+ const pdfBytes = await multiSignEmbed (
467
+ pngUrl ,
468
+ pdfDoc ,
469
+ pdfOriginalWidth ,
470
+ true
471
+ ) ;
557
472
signPdfFun ( pdfBytes , documentId ) ;
558
473
}
559
474
setIsSignPad ( false ) ;
@@ -574,28 +489,28 @@ function SignYourSelf() {
574
489
let singleSign ;
575
490
576
491
const isMobile = window . innerWidth < 767 ;
577
- const newWidth = window . innerWidth ;
492
+ const newWidth = window . innerWidth - 32 ;
578
493
const scale = isMobile ? pdfOriginalWidth / newWidth : 1 ;
579
494
const imgWidth = xyPosData ? xyPosData . Width : 150 ;
580
495
if ( xyPostion . length === 1 && xyPostion [ 0 ] . pos . length === 1 ) {
581
496
const height = xyPosData . Height ? xyPosData . Height : 60 ;
582
497
const bottomY = xyPosData . isDrag
583
- ? xyPosData . yBottom * scale - height
498
+ ? xyPosData . yBottom * scale - height * scale
584
499
: xyPosData . firstYPos
585
- ? xyPosData . yBottom * scale - height + xyPosData . firstYPos
586
- : xyPosData . yBottom * scale - height ;
500
+ ? xyPosData . yBottom * scale - height * scale + xyPosData . firstYPos
501
+ : xyPosData . yBottom * scale - height * scale ;
587
502
588
503
singleSign = {
589
504
pdfFile : pdfBase64Url ,
590
505
docId : documentId ,
591
506
sign : {
592
507
Base64 : base64Url ,
593
508
Left : isMobile
594
- ? xyPosData . xPosition * scale + imgWidth / 2
509
+ ? xyPosData . xPosition * scale + 43
595
510
: xyPosData . xPosition ,
596
511
Bottom : bottomY ,
597
- Width : xyPosData . Width ? xyPosData . Width : 150 ,
598
- Height : height ,
512
+ Width : xyPosData . Width ? xyPosData . Width * scale : 150 * scale ,
513
+ Height : height * scale ,
599
514
Page : pageNo
600
515
}
601
516
} ;
0 commit comments