There is no native way to define page breaks within a large SVG for printing—it either gets scaled down to fit on a single page or gets cut off. There are many situations where pagination would be useful, such as maps, flow charts, or org diagrams. Really, any large, complicated graphic that becomes incomprehensible if scaled too small.
This pen demonstrates a way to generate custom pagination by re-using the same graphic within multiple SVG elements, using the viewBox attribute to define which part of the graphic should be visible each time. Media queries control which SVGs are visible on screen or when printed.
Print preview or print to file to see the results.
(Getting the browser to print a single iframe within Codepen is a little tricky. For Firefox, use "This Frame > Print" (no preview option, you'll have to print to file). For IE, select the entire content of the results frame, use "Print Preview", and then choose "As selected on screen". For Chrome, I've only been able to get it to work with Debug mode (i.e., without iframes). Opera's the worst: no print preview, and no way to print a single frame.)
Benefits:
-
You can explicitly define the breaks, including whether you want overlap in coverage between pages (standard for maps, and demonstrated here).
-
You can add extra graphics or labels (with some work to get them positioned correctly), such as the map continuation arrows I've added here.
Downsides:
-
You have to explicitly define the breaks, for each print medium you want to support, and create separate markup for each page in each layout. Here, I've defined print and landscape layouts for standard letter/A4 page sizes, but if you were to print on larger paper you would get sub-optimal breaks.
-
Browser's CSS print layout algorithms are not as advanced as screen layout. Chrome doesn't support viewport units in print (they should represent the size of the print region on each page) and was collapsing the SVGs down to zero if I used them, so instead I've defined the print size in inches, which further limits the adaptability to other page formats.
-
Even then, Chrome and IE's efforts to break large graphics across pages causes problems when I try to scale down the graphics slightly for portrait mode. Using defined width and height and the viewBox attribute wasn't enough, I had to specifically set
page-break-inside: avoid
.
P.S. The layout of the text labels are a little wonky in FF and IE because these browsers don't support alignment-baseline: central
. You could achieve the same effect with dx/dy attributes on the text elements, but the markup is complex enough for an example.
A Pen by Amelia Bellamy-Royds on CodePen.