Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SVG images #23

Closed
ngns opened this issue Apr 22, 2016 · 15 comments
Closed

Add support for SVG images #23

ngns opened this issue Apr 22, 2016 · 15 comments

Comments

@ngns
Copy link

ngns commented Apr 22, 2016

Currently, SVG images are not supported. PDFBox does not "draw" SVGs natively. Best practice is, to use apache batik to transcode SVGs to PDFs and embed these as PDFFormXObject via PDFBox.

_See also _

@danfickle
Copy link
Owner

I decided to create our own transcoder rather than the transcoder provided by FOP. This gives the benefits that it can output to Java2D as well as pdf and can use our user agent, font resolver, etc. However, it is a bit more work so I'm still debugging it and will commit something tomorrow for testing.

@danfickle danfickle self-assigned this Apr 25, 2016
@ngns
Copy link
Author

ngns commented Apr 29, 2016

Should we keep the transcoder to use flexible? Batik is very mature, and will likely be quicker to implement for complex SVGs.
If your are going for Java2D target, maybe it could be built upon existing solutions like Flamingo SVG Transcoder (also batik-based)? http://ebourg.github.io/flamingo-svg-transcoder/

danfickle added a commit that referenced this issue Jun 11, 2016
This is a very early prototype using Batik to draw inline SVG graphics
in a PDF document. Still to do are images and text in the SVG. Also to
do is support for Java 2D.
@danfickle
Copy link
Owner

This is a screen shot showing simple SVG graphics in a PDF.
openhtmltopdf-screenshot

danfickle added a commit that referenced this issue Jun 12, 2016
…lso uses the first color in a SVG linear gradient rather than ignoring it.
@danfickle
Copy link
Owner

danfickle commented Jun 12, 2016

Todo for proper SVG output:

  • Basic fill and draw operations
  • Basic text output
  • Inline SVG in HTML document
  • External SVG in image
  • Linear gradient support
  • Alpha support
  • Custom font support
  • Insert raster image in SVG
  • SVG transform support
  • SVG clipping support
  • Testing

@danfickle
Copy link
Owner

With commit bb586a9 you can output custom fonts in SVG like this (text color is still wrong and gradients aren't implemented):

svg-custom-font

<html>
<head>
<meta charset="UTF-8"/>
<style>
@font-face {
  font-family: 'handwriting';
  src: url(JustAnotherHand.ttf);
}
svg {
    display: block;
}
body {
    font-size: 50px;
}
</style>
</head>
<body>
<svg height="130" width="500" xmlns="http://www.w3.org/2000/svg" version="1.1"
  xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  <text fill="#ffffff" font-size="45" font-family="handwriting" x="50" y="86">SVG</text>
</svg>
</body>
</html>

danfickle added a commit that referenced this issue Jun 18, 2016
- Correctly deal with width and height of SVG elements.
- Add svg as block element to the base stylesheet (only block elements
can be replaced).
- Import fonts once per document rather than for every SVG element.
danfickle added a commit that referenced this issue Jun 19, 2016
…bled due to issues with the transform origin.
danfickle added a commit that referenced this issue Jun 22, 2016
… origin.

At least when it comes to SVG transforms, we are now matching Chrome.
@rototor
Copy link
Contributor

rototor commented Sep 27, 2016

@danfickle I am not sure if the current approach is the right one. Instead of retrofitting a Graphics2D on a OutputDevice it should just be possible to get a Graphics2D to draw on. Like "createGraphics(rectangle)".

This was possible with iText, but is currently not possible with PdfBox. After I finish #38 I will implement such a PdfBoxGraphics2D, as I need that for some reports where we have Java2D drawing code for some diagrams. And I need text support including the ability to render text as vector paths. But if you want to build such a PdfBoxGraphics2D feel free to do so. The PdfBox seems not interested in such a graphics 2d...

@danfickle
Copy link
Owner

@rototor - I think you have a good point. In fact a standalone Graphics2D implementation which output to PDF would be useful for many projects, not just this one.

I found an old attempt and found some of our code is identical. I don't know who copied who. The old attempt would be useful for some of the maths, etc. It is also licensed under the LGPL.

http://gnujpdf.cvs.sourceforge.net/viewvc/gnujpdf/gnujpdf-1.7/src/gnu/jpdf/PDFGraphics.java?revision=1.6&view=markup

@brettclark
Copy link

Just curious, is any other image format supported by openhtmltopdf right now? For example, I have a PNG which I need to render.

@danfickle
Copy link
Owner

Hi @brettclark
PNG should work fine. Generally, any image format supported by Java's ImageIO should work including PNG, JPEG and GIF.

@rototor
Copy link
Contributor

rototor commented Jan 30, 2017

Just for your information: I started a Graphics2D for PDFBox (https://github.com/rototor/pdfbox-graphics2d) but it seems not to be that simple. I choose the Apache 2.0 Licence, cause maybe PDFBox may want to integrate it when its finished.

rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 2, 2017
…This implements

the infrastructure, but does not yet use it to render SVGs.
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 2, 2017
@rototor
Copy link
Contributor

rototor commented Feb 2, 2017

FYI: I've just created a pull request #66 which implements the SVG rendering using my Graphics2D adapter. It just does the placement of the final XForm wrong at the moment.

danfickle pushed a commit that referenced this issue Feb 11, 2017
* #23: Allow drawing on the output device using a Graphics2D. This implements
the infrastructure, but does not yet use it to render SVGs.

* #23: Use pdfbox-graphics2d to render the SVG. But the positioning is not
right yet.
danfickle added a commit that referenced this issue Feb 13, 2017
…oxGraphics2D by @rototor

- Converts XForm object from 72dpi to 96dpi using the XForm output
matrix.
- Fixes y to take into account the difference between bottom left
coordinate system and top left coordinate system.
- Apache Batik SVG renderer is still rendering in 96dpi on a 72dpi
device so this will form work for the next commit.
danfickle added a commit that referenced this issue Feb 13, 2017
@rototor - The SVG example is now working perfectly including text,
sizing and positioning. Custom fonts via the font-face rule are also
working.
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 19, 2017
…This implements

the infrastructure, but does not yet use it to render SVGs.
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 19, 2017
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 19, 2017
…itioning is

now right, so it is no longer needed.
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 19, 2017
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 19, 2017
…itioning is

now right, so it is no longer needed.
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 19, 2017
@danfickle
Copy link
Owner

danfickle commented Feb 21, 2017

Thanks to awesome library and work by @rototor SVG support is largely done.
Current known limitations:

  • Raster image URIs in SVGs (<image> tag) are not going through the URI resolver so only absolute URIs can be used.
  • External SVG images are not loaded yet.
  • Security is not yet assured (do scripts run in any circumstances?) so do not use user provided markup for SVGs (or HTML for that matter).

rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 26, 2017
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 26, 2017
@pilami
Copy link

pilami commented Mar 30, 2017

Can this be released to maven repo?

@danfickle
Copy link
Owner

@pilami
Hi, RC10 has been released to Maven now.

@jmsalcido
Copy link

@danfickle this can be closed now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants