-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
plotly-graph PDF with electron's printToPDF #6
Conversation
- adding util/remote makes the renderer's remote module easily mockable using sinon. - add stubProp common test util, to e.g. reduce iframeLoadDelay in tests
- when *batik* option isn't defined, use win.webContents.printToPDF - step-by-step: + use svg image string to render image in DOM + call window.getSelection.selectAllChidren to grab only img div + call printToPDF on img div
To me 👀 , For all images below: left: batik / right:
bounding boxed in general look a lot better in the Both outputs are wrong (they both show little gap(s)) for batik is bad at doing carpets: batik doesn't like
|
src/component/plotly-graph/render.js
Outdated
printSelectionOnly: true, | ||
pageSize: { | ||
width: (imgOpts.width) / 0.0035, | ||
height: (imgOpts.height) / 0.0035 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pageSize
here expects numbers in micrometer, plotly.js set width and height in pixels. I took this number from this source. I gives ok results, but not perfect.
Comparing the batik and electron outputs of a graph with non-white bg illustrates the issue:
batik on the left / printToPDF
on the right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit 5992a8b improves this.
But printToPDF
still output a (small white) margin:
Would this be a deal-breaker for printToPDF
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with 135cb1b
those white margins are gone (actually, they're still there, but now they appear with background-color: fullLayout.paper_bgcolor
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the deep dive into this one @etpinard - given all the other improvements we get with printToPDF
I think this extra bit of margin, now that it keeps the right color, is acceptable.
Might be worthwhile at some point taking another look at this but for the first release this is great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worthwhile at some point taking another look at this
For sure. I'll open an issue as soon as this PR is merged.
Thanks for the gist https://gist.github.com/etpinard/1ce4f6e4c089d5bfc426485638d0abda - nice work on the comparison. I am impressed how well the Electron I'll let @alexcjohnson's discerning 👀 scrutinize those pdfs and for the final judgement on that one :) |
I think so too, especially that the out-of-the-box is much faster 🐎 |
- which is functional on branch https://github.com/plotly/plotly.js/compare/to-image-scale
- only expose svg2pdf routine! - i.e. 🔪 eps logic (will be in own pdftops util) and scale logic (to be done in renderer hopefully)
by the way, |
- use correct px-by-micron logic - add margin offset (that appears to work ok)
Fonts (after installing them locally - ref #11) are looking sharp: |
Oh and I should add, switching to electron's Screenshot of |
Some vacation-delayed comments on the images above:
So much better to be generating PDFs in the same environment the fonts were originally rendered in - major win!
We can ignore this for now as it's not new, but would be worth investigating later. This is happening because we apply FYI
How did we miss these for so long? Nice wins for electron anyway... but I worry that svg exports brought into another environment (illustrator and its ilk?) might cause this to surface again - perhaps we could figure out what carpet & parcoords do differently here and make them match everything else?
Is it not getting the same
polar text gets a white shadow behind it, which seems to get corrupted by |
- open new window for each pdf export which: + 🔪 the extraneous margin-left in printSelectionOnly exports + no longer freezes up when parallelLimit > 1 - we still need to add a few pixel to the export pageSize + but now hide those artificial margin by setting and exporting the graphs' bgColor.
src/component/plotly-graph/render.js
Outdated
const pxByMicrometer = 0.00377957517575025 | ||
const inducedMarginOffset = 18 | ||
const offset = 6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still need this unfortunately to make sure the exported pdf always fits on one page.
Like I said to @alexcjohnson in a private convo, I believe electron (or chromium?) is messing up their pixel to micron computation somewhere. I should try to write up a bug report for them. Oh well, in the meantime hacks appears to work.
To see it the results:
npm i -g gistclone
gistclone https://gist.github.com/etpinard/1ce4f6e4c089d5bfc426485638d0abda
cd "plotly-graph PDF comparison"
<pdf-viewer-of-your-choice> all-electron.pdf
// we can't set image src into html as chromium has a 2MB URL limit | ||
// https://craignicol.wordpress.com/2016/07/19/excellent-export-and-the-chrome-url-limit/ | ||
|
||
win.loadURL(`data:text/html,${html}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting this to data:image/svg+xml,${imgData}
wasn't an option unfortunately as Chromium sets a limit of 2MB
for URLs, and yes, SVGs of size > 2MB
do happen.
We don't test PDF generation for all our mocks (just these mocks to be precise) and even then we only check if the export file size is bigger than some number. It might worth spending some time adding pdf diff test to our suite (e.g. using
Good point about Illustrator, we should check 🔬
This is probably the last blocking issue remaining (well this and the polar font thing, but it's polar so 😏 ). To make matters worse, those grid lines look different when generated with Hopefully tweaking |
@@ -30,6 +30,9 @@ module.exports = { | |||
|
|||
mathJaxConfigQuery: '?config=TeX-AMS-MML_SVG', | |||
|
|||
// config option passed in render step | |||
plotGlPixelRatio: 3, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* - https://xmlgraphics.apache.org/batik/tools/rasterizer.html | ||
* - http://archive.apache.org/dist/xmlgraphics/batik/ | ||
*/ | ||
class Batik { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One for the ages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🌅 It's a brand new day 💯
1aeea7b
to
880ffd3
Compare
- 🔪 plotly-graph-exporter stdout msg test (for now, this fails on CI)
4b18d37
to
11b67c3
Compare
@@ -0,0 +1,33 @@ | |||
version: 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle 2.0 is blazing fast 🐎 - see https://circleci.com/docs/2.0/
Ok. I gonna go ahead and merge this thing to keep the wheels spinning. Feel free to open an issue if you want to discuss anything |
See commit 54b4173 for the details and see comparison with the batik outputs here.
Note that, electron's
printToPDF
is way faster too. Generating all 411 mocks took about 8 minutes using batik and just under 3 minutes usingprintToPDF
.