printpdf icon indicating copy to clipboard operation
printpdf copied to clipboard

SVG example

Open drager opened this issue 8 years ago • 5 comments

Hey,

Would be great with a example for rendering SVG.

drager avatar Aug 11 '17 10:08 drager

Embedding SVG doesn't work yet. You can use the svg crate, iterate over the paths and then use build a Vec<Point> from those. This functionality should be moved into the library later on, for drawing symbols. I'll get it done some time later. Thanks for the reminder.

fschutt avatar Aug 11 '17 11:08 fschutt

Ah I see, would it be good to have an example of using the svg with printpdf?

drager avatar Aug 11 '17 11:08 drager

Yeah it's in the code already, it just doesn't have an interface to the LayerReference, because I didn't have time: https://github.com/sharazam/printpdf/blob/master/src/types/plugins/graphics/two_dimensional/svg.rs

There is a commented-out function add_svg() already: https://github.com/sharazam/printpdf/blob/master/src/types/pdf_layer.rs#L83 but this is for library-internal use.

Since SVG elements are often repeated on a page, I tried seperating them out into a FormXObject, but I failed so far (PDF errors). I'll definitely try again later on. You could remove the pub(crate) things and try it at your own risk. Metrics in SVG are hard.

Not sure if it works, didn't test it. Code is half taken from the example code that svg provides.

fschutt avatar Aug 11 '17 11:08 fschutt

Alright, I can try that out. Thank you! :)

drager avatar Aug 17 '17 05:08 drager

I'll add SVG later on. Right now my workaround is to put the SVG element in a font (manually) and then use the font. For "proper" SVG support you'll probably also need gradients. But for monochrome symbols, the font approach works, too.

fschutt avatar Aug 17 '17 06:08 fschutt