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

✨ IMPROVE: Styling print to pdf #438

Merged
merged 17 commits into from
Dec 26, 2021

Conversation

AakashGfude
Copy link
Member

@AakashGfude AakashGfude commented Dec 9, 2021

The following PR aims to change the structure and styling of single page documents when printing using "Print to PDF" functionality of jb. It primarily relies on two css classes for defining structure(showing, hiding elements from the dom) - onlyprint and noprint.

Output from this PR looks like this:

Screen Shot 2021-12-15 at 2 08 00 pm

Store code outputs and insert into content.pdf

The above document's content is from https://jupyterbook.org/content/executable/output-insert.html

Will keep updating the output picture and document here, as we push more changes.

EDIT 1: increased the width content, and handled margin directive as shown in the pic above.
cc: @rowanc1 @damianavila @mmcky

@AakashGfude AakashGfude requested a review from choldgraf December 9, 2021 22:31
@mmcky
Copy link
Member

mmcky commented Dec 9, 2021

thanks @AakashGfude the new single-page formatting looks nice and a lot less like a "printed" webpage.

The link to the pdf doesn;t seem to work - just FYI.

@AakashGfude
Copy link
Member Author

The link to the pdf doesn;t seem to work - just FYI.

Thanks @mmcky , should be working now.

@mmcky
Copy link
Member

mmcky commented Dec 9, 2021

Just some thoughts:

  1. might it be possible to add an "option" to turn off "margin notes". (in the theme) They often aren't essential to the thread of a page and it might be nice to use the full width of the page.
  2. Can we remove "Print to PDF"?

@AakashGfude
Copy link
Member Author

AakashGfude commented Dec 9, 2021

Just some thoughts:

  1. might it be possible to add an "option" to turn off "margin notes". (in the theme) They often aren't essential to the thread of a page and it might be nice to use the full width of the page.

Good idea. We can also style the margin to include it in the main content instead of in the side, and stylize it in a way to make it look like a note?

  1. Can we remove "Print to PDF"?

Working on this.

@AakashGfude
Copy link
Member Author

  1. Can we remove "Print to PDF"?

It seems like as long as there is a title tag on the button:

<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="{{ translate('Print to PDF') }}"
            onClick="window.print()" data-toggle="tooltip" data-placement="left">.pdf</button>

the print to PDF is stuck in the pdf output.

Even trying to detach the window.print() function from button tag does not help:

<a href="javascript:printPage()"><button type="button" id="download-print" class="btn btn-secondary topbarbtn noprint" title="{{ translate('Print to PDF') }}"
                data-toggle="tooltip" data-placement="left">.pdf</button></a>

Here, printPage calls the window.print() function.

@AakashGfude
Copy link
Member Author

One more drawback is that background-color property is not shown unless clicking the checkbox of background graphics .

Ex:
(Left: on, Right: off)
Screen Shot 2021-12-10 at 5 11 38 pm
Screen Shot 2021-12-10 at 5 23 52 pm
Screen Shot 2021-12-10 at 5 12 24 pm
Screen Shot 2021-12-10 at 5 24 38 pm

Copy link
Member

@choldgraf choldgraf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice improvement! I have a few thoughts in there about simplifying the codebase a little bit, but in general I think this is close.

The "print to PDF" thing is annoying, though already present in the theme (I didn't notice that before), so I don't think it should block this PR. Though @AakashGfude if you can figure out how to avoid it, that would be fantastic. Note that I believe this is using bootstrap-native tooltips, so maybe those docs will have helpful pointers: https://getbootstrap.com/docs/4.0/components/tooltips/

@@ -20,7 +20,25 @@
<main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
{% block docs_body %}
{% include "topbar.html" %}
<div id="main-content" class="row">
<h1 class="onlyprint">{{ pagetitle }}</h1>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just wrap all of this in a div like <div id="jb-print-wrapper" class="onlyprint">? I feel like that'd be the most obvious to understand and then we only need one onlyprint in there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this were the case, could we just put the TOC code:

https://github.com/executablebooks/sphinx-book-theme/blob/master/sphinx_book_theme/topbar.html#L21-L34

in a standalone html file, and then import it there and here, so we don't repeat ourselves?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@choldgraf there is a bit of difference in classes between the toc in print and non-print.

<div>
<h2> {{ translate(theme_toc_title) }} </h2>
</div>
<nav aria-label="Page">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably don't need aria labels and such because this is being printed to PDF, so screen readers won't make sense, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@choldgraf these lectures, I reckon, can also be just downloaded as PDF and read on a computer or tablet offline instead of printing it in particular? In which case, screen readers would be important. Not sure, if this HTML tag translates to pdf output as well though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either - but if we think it might help folks with a11y issues, let's just leave it in!

@AakashGfude
Copy link
Member Author

AakashGfude commented Dec 13, 2021

Note that I believe this is using bootstrap-native tooltips, so maybe those docs will have helpful pointers: https://getbootstrap.com/docs/4.0/components/tooltips/

Thank you @choldgraf for the link. Though I could not find any resource to remove it in a boostrapy way. But I did get the element ids. I am detaching them from the DOM just before calling window.print() and then reattaching them again for HTML. A bit hacky but works nicely. Have updated the description image.

Copy link
Member

@choldgraf choldgraf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple quick comments, this looks nice!

</div>
</div>
</div>
<div id="main-content" class="row noprint">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused - wouldn't this mean that the whole #main-content div doesn't show up when printing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This #main-content won't, but this one will https://github.com/executablebooks/sphinx-book-theme/pull/438/files#diff-6cf877d43a0155d9e3706cb22f143f326d4283458bfdcac93017b197c4f02e2bR27 . Looking at this now, i should rename the id of onlyprint one to something else, as there are two main-content ids at one time in this page.

Copy link
Member Author

@AakashGfude AakashGfude Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe convert main-content to a class name instead of id ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm - it seems reasonable to use a unique ID for the print-only header. That seems like a smaller change than converting it from an ID to a class, IMO

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Changed them to individual ids.

@AakashGfude
Copy link
Member Author

AakashGfude commented Dec 15, 2021

@choldgraf this looks ready to go I think, if there are no further comments.

Copy link
Member

@choldgraf choldgraf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lost track of this one, but thanks @AakashGfude for the improvements! Let's iterate in subsequent PRs if we find other improvements to make.

@choldgraf choldgraf merged commit 37f0e35 into executablebooks:master Dec 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants