Is it possible to render shadow effect using pdfkit?

1.1k Views Asked by At

I used javascript pdfkit bower package to render SVG to PDF. All things working fine but shadow effect is not rendering.

SVG code (rendered from fabricjs)

<filter id="SVGID_32" y="-22%" height="144%" x="-21%" width="142%" >
    <feGaussianBlur in="SourceAlpha" stdDeviation="1"></feGaussianBlur>
    <feOffset dx="-2" dy="0" result="oBlur" ></feOffset>
    <feFlood flood-color="rgba(0,0,0,1)"/>
    <feComposite in2="oBlur" operator="in" />
    <feMerge>
        <feMergeNode></feMergeNode>
        <feMergeNode in="SourceGraphic"></feMergeNode>
    </feMerge>
</filter>
    <g transform="translate(588.56 1434.18) scale(1.06 1.06)" style="filter: url(#SVGID_32);">
        <text xml:space="preserve" font-family="us101" font-size="93.33333333333334" font-style="italic" font-weight="400" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(236,236,236); fill-rule: nonzero; opacity: 1; white-space: pre;" >
            <tspan x="-378.71" y="29.32" >AAAAA</tspan>
        </text>
    </g>

Is there anyone who have experience in rendering shadow when generating pdf file? Great thanks for any comment.

UPDATE:

javascript code:

doc = new PDFDocument({
    layout: 'landscape',
    size: [docHeight, docWidth]
});
setTimeout(function() {
    var fd = new FormData();
    var div = document.createElement('div');
    div.innerHTML = '<svg ' + canvas.fabric.toSVG().split('<svg ')[1];
    var element = div.firstChild;
    SVGtoPDF(doc, element, 0, 0, { useCSS: true });
    stream = doc.pipe(blobStream());       
    stream.on('finish', function() {
        var blob = stream.toBlob('application/pdf');

        fd.append("pdfimage", blob);
        $.ajax({
            ...
        });
    });
doc.end();
2

There are 2 best solutions below

0
On BEST ANSWER

There are no shadows in svg, nor svg supported in pdf explicitly. I see you are using fabricjs to generate the SVG. Fabric uses filters (blur and offset ) to emulate a shadow effect.

What SVGtoPDF probably do is parsing the paths and draw the same commanda in the ps language for pdf. I think filters are not supported at all.

0
On

I just added shadow by adding shadow pictures manually. This is picture generating code.

canvas._objects.forEach(function(obj, index){
  if(obj.shadow && obj.type == 'i-text'){
    objForm = obj.toObject();
    var Duplicated_obj = new fabric.IText(objForm.text, objForm);
    Duplicated_obj.fill = "rgba('50,50,50,0.8')";
    fabric.util.loadImage(Duplicated_obj.toDataURL({format:'png'}),function(image){
       var object = new fabric.Image(image);
       object.name = 'shadowImage???';
       object.left = obj.left;
       object.top = obj.top;
       canvas.insertAt(object,index-1);
    });
  }
});