alafr / SVG-to-PDFKit

Insert SVG into a PDF document created with PDFKit
MIT License
404 stars 115 forks source link

Is there a quick and dirty way to export pre-made pdfkit code from svg-to-pdfkit? #117

Closed JakeTrock closed 4 years ago

JakeTrock commented 4 years ago

I'm working on a system which would convert an array of names and a svg file of a certificate into a multi-page pdf of certificates. Is there any way to intercept the function calls between your code and pdfkit to get raw pdfkit code, so I could easily tweak it? I've already tried proxying the class, and reading over your code but I can't make heads or tails of it. All I'd like to know is where to put a console.log statement to capture all of the calls and their contents. For example, if svg-to-pdfKit called doc.text("blah"), the console would read text,"blah"

alafr commented 4 years ago

Where to put a console.log statement? Where there is the "doc" variable in the source code (almost everywhere...). The huge difficulty is what to log...

Not every interaction with pdfkit is a simple call like doc.some_function(args).

Every call to doc.ref(), doc.linearGradient() or doc.radialGradient() will be a problem, because these functions return objects (PDFReference, PDFGradient) which also have their own functions. Also these objects are non-serializable and are sometimes used as arguments in other calls. Theses special objects should be also proxied and kept in temporary variables (there can be many PDFReference at the same moment...). It may be possible but definitely not easy / quick!

As an example, for the creation of an empty group (source.js#L66-L104), those lines should be logged, so that it can be recreated with the eval() function:

var1 = doc.ref()
var2 = doc.ref({Type: 'XObject', Subtype: 'Form', FormType: 1, BBox: bbox,
                  Group: {S: 'Transparency', CS: 'DeviceRGB', I: true, K: false}, Resources: var1});
var2.write('')
var3 = doc.page.font
var1.data.Font = var3
var4 = doc.page.xobjects
var1.data.XObject = var4
var5 = doc.page.ext_gstates
var1.data.ExtGState = var5
var6 = doc.page.patterns
var1.data.Pattern = var6
var1.end()
var2.end()
doc.page.xobjects.G1 = var2
doc.addContent('/G1 Do')

Another difficulty will be to distinguish the calls made by svg-to-pdfkit from those made by pdfkit (for example doc.fillColor internally calls doc._setColor, doc.fillOpacity and doc._fillColor)

Isn't it easier to create a svg file with some ____NAME____ placeholder at the desired location and use something like svg_string.replace("____NAME____ ", the_real_name) ?

JakeTrock commented 4 years ago

I actually do what you suggested at the bottom, but it has grown far too cumbersome, as I used a different computer for adobe illustrator, which is now inaccessible due to coronavirus, something as simple as changing the color of the accent border has now become close to impossible because I have no software to do so. In my struggle to log calls, I have run into this problem you mentioned, the giant doc variable being logged. I think I'll just get the names of all methods called, and attach debuggers to them. Thank you for the help nontheless