-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmydoc_generating_pdfs.html
More file actions
667 lines (460 loc) · 44.1 KB
/
mydoc_generating_pdfs.html
File metadata and controls
667 lines (460 loc) · 44.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="You can generate a PDF from your Jekyll project. You do this by creating a web version of your project that is printer friendly. You then use utility called ...">
<meta name="keywords" content="publishingsingle_sourcingcontent_types, PDF, prince, prince XML, ant, xsl fo">
<title>Generating PDFs | IB-Ruby documentation</title>
<link rel="stylesheet" href="css/syntax.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!--<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">-->
<link rel="stylesheet" href="css/modern-business.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="css/customstyles.css">
<link rel="stylesheet" href="css/boxshadowproperties.css">
<!-- most color styles are extracted out to here -->
<link rel="stylesheet" href="css/theme-blue.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="js/jquery.navgoco.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Anchor.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.2.0/anchor.min.js"></script>
<script src="js/toc.js"></script>
<script src="js/customscripts.js"></script>
<link rel="shortcut icon" href="images/favicon.ico">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<link rel="alternate" type="application/rss+xml" title="documentation-theme-jekyll" href="https://github.com/feed.xml">
<script>
$(document).ready(function() {
// Initialize navgoco with default options
$("#mysidebar").navgoco({
caretHtml: '',
accordion: true,
openClass: 'active', // open
save: false, // leave false or nav highlighting doesn't work right
cookie: {
name: 'navgoco',
expires: false,
path: '/'
},
slide: {
duration: 400,
easing: 'swing'
}
});
$("#collapseAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', false);
});
$("#expandAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', true);
});
});
</script>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
<script>
$(document).ready(function() {
$("#tg-sb-link").click(function() {
$("#tg-sb-sidebar").toggle();
$("#tg-sb-content").toggleClass('col-md-9');
$("#tg-sb-content").toggleClass('col-md-12');
$("#tg-sb-icon").toggleClass('fa-toggle-on');
$("#tg-sb-icon").toggleClass('fa-toggle-off');
});
});
</script>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container topnavlinks">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="fa fa-home fa-lg navbar-brand" href="index.html"> <span class="projectTitle"> IB-Ruby Doc</span></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<!-- toggle sidebar button -->
<li><a id="tg-sb-link" href="#"><i id="tg-sb-icon" class="fa fa-toggle-on"></i> Nav</a></li>
<!-- entries without drop-downs appear here -->
<li><a href="https://github.com/ib-ruby" target="_blank" rel="noopener">GitHub</a></li>
<li><a href="news">News</a></li>
<!-- entries with drop-downs appear here -->
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
<!--comment out this block if you want to hide search-->
<li>
<!--start search-->
<div id="search-demo-container">
<input type="text" id="search-input" placeholder="search...">
<ul id="results-container"></ul>
</div>
<script src="js/jekyll-search.js" type="text/javascript"></script>
<script type="text/javascript">
SimpleJekyllSearch.init({
searchInput: document.getElementById('search-input'),
resultsContainer: document.getElementById('results-container'),
dataSource: 'search.json',
searchResultTemplate: '<li><a href="{url}" title="Generating PDFs">{title}</a></li>',
noResultsText: 'No results found.',
limit: 10,
fuzzy: true,
})
</script>
<!--end search-->
</li>
</ul>
</div>
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<div id="main">
<!-- Content Row -->
<div class="row">
<!-- Sidebar Column -->
<div class="col-md-3" id="tg-sb-sidebar">
<ul id="mysidebar" class="nav">
<li class="sidebarTitle"> </li>
<!-- if you aren't using the accordion, uncomment this block:
<p class="external">
<a href="#" id="collapseAll">Collapse All</a> | <a href="#" id="expandAll">Expand All</a>
</p>
-->
</ul>
<!-- this highlights the active parent class in the navgoco sidebar. this is critical so that the parent expands when you're viewing a page. This must appear below the sidebar code above. Otherwise, if placed inside customscripts.js, the script runs before the sidebar code runs and the class never gets inserted.-->
<script>$("li.active").parents('li').toggleClass("active");</script>
</div>
<!-- Content Column -->
<div class="col-md-9" id="tg-sb-content">
<div class="post-header">
<h1 class="post-title-main">Generating PDFs</h1>
</div>
<div class="post-content">
<div class="summary">You can generate a PDF from your Jekyll project. You do this by creating a web version of your project that is printer friendly. You then use utility called Prince to iterate through the pages and create a PDF from them. It works quite well and gives you complete control to customize the PDF output through CSS, including page directives and dynamic tags from Prince.</div>
<!-- this handles the automatic toc. use ## for subheads to auto-generate the on-page minitoc. if you use html tags, you must supply an ID for the heading element in order for it to appear in the minitoc. -->
<script>
$( document ).ready(function() {
// Handler for .ready() called.
$('#toc').toc({ minimumHeaders: 0, listType: 'ul', showSpeed: 0, headers: 'h2,h3,h4' });
/* this offset helps account for the space taken up by the floating toolbar. */
$('#toc').on('click', 'a', function() {
var target = $(this.getAttribute('href'))
, scroll_target = target.offset().top
$(window).scrollTop(scroll_target - 10);
return false
})
});
</script>
<div id="toc"></div>
<h2 id="pdf-overview">PDF overview</h2>
<p>This process for creating a PDF relies on Prince XML to transform the HTML content into PDF. Prince costs about $500 per license. That might seem like a lot, but if you’re creating a PDF, you’re probably working for a company that sells a product, so you likely have access to some resources. There’s also a free license that prints a small “P” watermark on your title page, so if you’re fine with that, great.</p>
<p>The basic approach is to generate a list of all web pages that need to be added to the PDF, and then add leverage Prince to package them up into a PDF. Once you set it up, building a pdf is just a matter of running a couple of commands. Also, creating a PDF this way gives you a lot more control and customization capabilities than with other methods for creating PDFs. If you know CSS, you can entirely customize the output.</p>
<h2 id="demo">Demo</h2>
<p>You can see an example of the finished product here:</p>
<p><a target="\_blank" class="noCrossRef" href="pdf/mydoc.pdf"><button type="button" class="btn btn-default" aria-label="Left Align"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> PDF Download</button></a></p>
<p>To generate the PDF, browse to the theme’s directory in your terminal and run this script:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">.</span> pdf-mydoc.sh
</code></pre></div></div>
<p>This builds a PDF for the documentation in the theme. Look in the <strong>pdf</strong> folder for the output, and see the “last generated date” to confirm that you generated the PDF.</p>
<p>To build a PDF for the other sample projects, run these commands:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">.</span> pdf-product1.sh
</code></pre></div></div>
<p>or</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">.</span> pdf-product2.sh
</code></pre></div></div>
<p>You can see the details of the script in these files in the theme’s root directory. For example, open pdf-mydoc.sh. It contains the following:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># Note that .sh scripts work only on Mac. If you're on Windows, install Git Bash and use that as your client.</span>
<span class="nb">echo</span> <span class="s1">'Kill all Jekyll instances'</span>
<span class="nb">kill</span> <span class="nt">-9</span> <span class="si">$(</span>ps aux | <span class="nb">grep</span> <span class="s1">'[j]ekyll'</span> | <span class="nb">awk</span> <span class="s1">'{print $2}'</span><span class="si">)</span>
clear
<span class="nb">echo</span> <span class="s2">"Building PDF-friendly HTML site for Mydoc ..."</span><span class="p">;</span>
bundle <span class="nb">exec </span>jekyll serve <span class="nt">--detach</span> <span class="nt">--config</span> _config.yml,pdfconfigs/config_mydoc_pdf.yml<span class="p">;</span>
<span class="nb">echo</span> <span class="s2">"done"</span><span class="p">;</span>
<span class="nb">echo</span> <span class="s2">"Building the PDF ..."</span><span class="p">;</span>
prince <span class="nt">--javascript</span> <span class="nt">--input-list</span><span class="o">=</span>_site/pdfconfigs/prince-list.txt <span class="nt">-o</span> pdf/mydoc.pdf<span class="p">;</span>
<span class="nb">echo</span> <span class="s2">"Done. Look in the pdf directory to see if it printed successfully."</span>
</code></pre></div></div>
<p>After stopping all Jekyll instances, we build Jekyll using a special configuration file that specifies a unique stylesheet. The build contains a file (prince-list.txt) that contains a list of all pages to be included in the PDF. We feed this list into a Prince command to build the PDF.</p>
<p>The following sections explain more about the setup.</p>
<h2 id="1-set-up-prince">1. Set up Prince</h2>
<p>Download and install <a href="http://www.princexml.com/doc/installing/">Prince</a>.</p>
<p>You can install a fully functional trial version. The only difference is that the title page will have a small Prince PDF watermark.</p>
<h2 id="2-create-a-new-configuration-file-for-each-of-your-pdf-targets">2. Create a new configuration file for each of your PDF targets</h2>
<p>The PDF configuration file will build on the settings in the regular configuration file but will some additional fields. Here’s the configuration file for the mydoc product within this theme. This configuration file is located in the pdfconfigs folder.</p>
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">destination</span><span class="pi">:</span> <span class="s">_site/</span>
<span class="na">url</span><span class="pi">:</span> <span class="s2">"</span><span class="s">http://127.0.0.1:4010"</span>
<span class="na">baseurl</span><span class="pi">:</span> <span class="s2">"</span><span class="s">/mydoc-pdf"</span>
<span class="na">port</span><span class="pi">:</span> <span class="m">4010</span>
<span class="na">output</span><span class="pi">:</span> <span class="s">pdf</span>
<span class="na">product</span><span class="pi">:</span> <span class="s">mydoc</span>
<span class="na">print_title</span><span class="pi">:</span> <span class="s">Jekyll theme for documentation — mydoc product</span>
<span class="na">print_subtitle</span><span class="pi">:</span> <span class="s">version </span><span class="m">5.0</span>
<span class="na">output</span><span class="pi">:</span> <span class="s">pdf</span>
<span class="na">defaults</span><span class="pi">:</span>
<span class="pi">-</span>
<span class="na">scope</span><span class="pi">:</span>
<span class="na">path</span><span class="pi">:</span> <span class="s2">"</span><span class="s">"</span>
<span class="na">type</span><span class="pi">:</span> <span class="s2">"</span><span class="s">pages"</span>
<span class="na">values</span><span class="pi">:</span>
<span class="na">layout</span><span class="pi">:</span> <span class="s2">"</span><span class="s">page_print"</span>
<span class="na">comments</span><span class="pi">:</span> <span class="no">true</span>
<span class="na">search</span><span class="pi">:</span> <span class="no">true</span>
<span class="na">pdf_sidebar</span><span class="pi">:</span> <span class="s">mydoc_sidebar</span>
</code></pre></div></div>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>::</b> Although you’re creating a PDF, you must still build an HTML web target before running Prince. Prince will pull from the HTML files and from the file-list for the TOC.</div>
<p>Note that the default page layout specified by this configuration file is <code class="language-plaintext highlighter-rouge">page_print</code>. This layout strips out all the sections that shouldn’t appear in the print PDF, such as the sidebar and top navigation bar.</p>
<p>Also note that there’s a <code class="language-plaintext highlighter-rouge">output: pdf</code> property in case you want to make some of your content unique to PDF output. For example, you could add conditional logic that checks whether <code class="language-plaintext highlighter-rouge">site.output</code> is <code class="language-plaintext highlighter-rouge">pdf</code> or <code class="language-plaintext highlighter-rouge">web</code>. If it’s <code class="language-plaintext highlighter-rouge">pdf</code>, then include information only for the PDF, and so on. If you’re using nav tabs, you’ll definitely want to create an alternative experience in the PDF.</p>
<p>In the configuration file, customize the values for the <code class="language-plaintext highlighter-rouge">print_title</code> and <code class="language-plaintext highlighter-rouge">print_subtitle</code> that you want. These will appear on the title page of the PDF.</p>
<p>We will access this configure file in the PDF generation script.</p>
<h2 id="3-make-sure-your-sidebar-data-file-has-titlepagehtml-and-tocpagehtml-entries">3. Make sure your sidebar data file has titlepage.html and tocpage.html entries</h2>
<p>There are two template pages in the root directory that are critical to the PDF:</p>
<ul>
<li>titlepage.html</li>
<li>tocpage.html</li>
</ul>
<p>These pages should appear in your sidebar YML file (in this product, mydoc_sidebar.yml):</p>
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="pi">-</span> <span class="na">title</span><span class="pi">:</span>
<span class="na">output</span><span class="pi">:</span> <span class="s">pdf</span>
<span class="na">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
<span class="na">folderitems</span><span class="pi">:</span>
<span class="pi">-</span> <span class="na">title</span><span class="pi">:</span>
<span class="na">url</span><span class="pi">:</span> <span class="s">/titlepage.html</span>
<span class="na">output</span><span class="pi">:</span> <span class="s">pdf</span>
<span class="na">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
<span class="pi">-</span> <span class="na">title</span><span class="pi">:</span>
<span class="na">url</span><span class="pi">:</span> <span class="s">/tocpage.html</span>
<span class="na">output</span><span class="pi">:</span> <span class="s">pdf</span>
<span class="na">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
</code></pre></div></div>
<p>Leave these pages here in your sidebar. (The <code class="language-plaintext highlighter-rouge">output: pdf</code> property means they won’t appear in your online TOC because the conditional logic of the sidebar.html checks whether <code class="language-plaintext highlighter-rouge">web</code> is equal to <code class="language-plaintext highlighter-rouge">pdf</code> or not before including the item in the web version of the content.)</p>
<p>The code in the tocpage.html is mostly identical to that of the sidebar.html page. This is essential for Prince to create the page numbers correctly with cross references.</p>
<p>There’s another file (in the root directory of the theme) that is critical to the PDF generation process: prince-list.txt. This file simply iterates through the items in your sidebar and creates a list of links. Prince will consume the list of links from prince-list.txt and create a running PDF that contains all of the pages listed, with appropriate cross references and styling for them all.</p>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>::</b> If you have any files that you do not want to appear in the PDF, add <code>output: web</code> (rather than <code>output: pdf</code>) in the list of attributes in your sidebar. The prince-list.txt file that loops through the mydoc_sidebar.yml file to grab the URLs of each page that should appear in the PDF will skip over any items that do not list <code>output: pdf</code> in the item attributes. For example, you might not want your tag archives to appear in the PDF, but you probably will want to list them in the online help navigation.</div>
<h2 id="4-customize-your-headers-and-footers">4. Customize your headers and footers</h2>
<p>Open up the css/printstyles.css file and customize what you want for the headers and footers. At the very least, customize the email address (<code class="language-plaintext highlighter-rouge">youremail@domain.com</code>) that appears in the bottom left.</p>
<p>Exactly how the print styling works here is pretty nifty. You don’t need to understand the rest of the content in this section unless you want to customize your PDFs to look different from what I’ve configured. But I’m adding this information here in case you want to understand how to customize the look and feel of the PDF output.</p>
<p>This style creates a page reference for a link:</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">]</span><span class="nd">::after</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">" (page "</span> <span class="n">target-counter</span><span class="p">(</span><span class="n">attr</span><span class="p">(</span><span class="n">href</span><span class="p">),</span> <span class="n">page</span><span class="p">)</span> <span class="s1">")"</span>
<span class="p">}</span>
</code></pre></div></div>
<p>You don’t want cross references for any link that doesn’t reference another page, so this style specifies that the content after should be blank:</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">*=</span><span class="s1">"mailto"</span><span class="o">]</span><span class="nd">::after</span><span class="o">,</span> <span class="nt">a</span><span class="o">[</span><span class="nt">data-toggle</span><span class="o">=</span><span class="s1">"tooltip"</span><span class="o">]</span><span class="nd">::after</span><span class="o">,</span> <span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">]</span><span class="nc">.noCrossRef</span><span class="nd">::after</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">""</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<div class="alert alert-info" role="alert"><i class="fa fa-bars"></i> If you have a link to a file download, or some other link that shouldn’t have a cross reference (such as link used in JavaScript for navtabs or collapsible sections, for example, add <code class="language-plaintext highlighter-rouge">noCrossRef</code> as a class to the link to avoid having it say “page 0” in the cross reference.</div>
<p>This style specifies that after links to web resources, the URL should be inserted instead of the page number:</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">^=</span><span class="s1">"http:"</span><span class="o">]</span><span class="nd">::after</span><span class="o">,</span> <span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">^=</span><span class="s1">"https:"</span><span class="o">]</span><span class="nd">::after</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">" ("</span> <span class="n">attr</span><span class="p">(</span><span class="n">href</span><span class="p">)</span> <span class="s1">")"</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>This style sets the page margins:</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">@page</span> <span class="p">{</span>
<span class="nl">margin</span><span class="p">:</span> <span class="m">60pt</span> <span class="m">90pt</span> <span class="m">60pt</span> <span class="m">90pt</span><span class="p">;</span>
<span class="nl">font-family</span><span class="p">:</span> <span class="nb">sans-serif</span><span class="p">;</span>
<span class="nl">font-style</span><span class="p">:</span><span class="nb">none</span><span class="p">;</span>
<span class="nl">color</span><span class="p">:</span> <span class="no">gray</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>To set a specific style property for a particular page, you have to name the page. This allows Prince to identify the page.</p>
<p>First you add frontmatter to the page that specifies the type. For the titlepage.html, here’s the frontmatter:</p>
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">---</span>
<span class="na">type</span><span class="pi">:</span> <span class="s">title</span>
<span class="nn">---</span>
</code></pre></div></div>
<p>For the tocpage, here’s the frontmatter:</p>
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">---</span>
<span class="na">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
<span class="nn">---</span>
</code></pre></div></div>
<p>For the index.html page, we have this type tag (among others):</p>
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">---</span>
<span class="na">type</span><span class="pi">:</span> <span class="s">first_page</span>
<span class="nn">---</span>
</code></pre></div></div>
<p>The default_print.html layout will change the class of the <code class="language-plaintext highlighter-rouge">body</code> element based on the type value in the page’s frontmatter:</p>
<div class="language-liquid highlighter-rouge"><div class="highlight"><pre class="highlight"><code><body class="<span class="p">{%</span><span class="w"> </span><span class="kr">if</span><span class="w"> </span><span class="nv">page</span><span class="p">.</span><span class="nv">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"title"</span><span class="p">%}</span>title<span class="p">{%</span><span class="w"> </span><span class="kr">elsif</span><span class="w"> </span><span class="nv">page</span><span class="p">.</span><span class="nv">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"frontmatter"</span><span class="w"> </span><span class="p">%}</span>frontmatter<span class="p">{%</span><span class="w"> </span><span class="kr">elsif</span><span class="w"> </span><span class="nv">page</span><span class="p">.</span><span class="nv">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"first_page"</span><span class="w"> </span><span class="p">%}</span>first_page<span class="p">{%</span><span class="w"> </span><span class="kr">endif</span><span class="w"> </span><span class="p">%}</span> print">
</code></pre></div></div>
<p>Now in the css/printstyles.css file, you can assign a page name based on a specific class:</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">body</span><span class="nc">.title</span> <span class="p">{</span> <span class="nl">page</span><span class="p">:</span> <span class="n">title</span> <span class="p">}</span>
</code></pre></div></div>
<p>This means that for content inside of <code class="language-plaintext highlighter-rouge">body class="title"</code>, we can style this page in our stylesheet using <code class="language-plaintext highlighter-rouge">@page title</code>.</p>
<p>Here’s how that title page is styled:</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">@page</span> <span class="n">title</span> <span class="p">{</span>
<span class="k">@top-left</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">@top-right</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span>
<span class="p">}</span>
<span class="k">@bottom-right</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">@bottom-left</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>As you can see, we don’t have any header or footer content, because it’s the title page.</p>
<p>For the tocpage.html, which has the <code class="language-plaintext highlighter-rouge">type: frontmatter</code>, this is specified in the stylesheet:</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">body</span><span class="nc">.frontmatter</span> <span class="p">{</span> <span class="nl">page</span><span class="p">:</span> <span class="n">frontmatter</span> <span class="p">}</span>
<span class="nt">body</span><span class="nc">.frontmatter</span> <span class="p">{</span><span class="nl">counter-reset</span><span class="p">:</span> <span class="n">page</span> <span class="m">1</span><span class="p">}</span>
<span class="k">@page</span> <span class="n">frontmatter</span> <span class="p">{</span>
<span class="k">@top-left</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">guideName</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">@top-right</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">datestamp</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">@bottom-right</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="n">counter</span><span class="p">(</span><span class="n">page</span><span class="p">,</span> <span class="nb">lower-roman</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">@bottom-left</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">"youremail@domain.com"</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>With <code class="language-plaintext highlighter-rouge">counter(page, lower-roman)</code>, we reset the page count to 1 so that the title page doesn’t start the count. Then we also add some header and footer info. The page numbers start counting in lower-roman numerals.</p>
<p>Finally, for the first page (which doesn’t have a specific name), we restart the counting to 1 again and this time use regular numbers.</p>
<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">body</span><span class="nc">.first_page</span> <span class="p">{</span><span class="nl">counter-reset</span><span class="p">:</span> <span class="n">page</span> <span class="m">1</span><span class="p">}</span>
<span class="nt">h1</span> <span class="p">{</span> <span class="nl">string-set</span><span class="p">:</span> <span class="n">doctitle</span> <span class="n">content</span><span class="p">()</span> <span class="p">}</span>
<span class="k">@page</span> <span class="p">{</span>
<span class="k">@top-left</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="n">string</span><span class="p">(</span><span class="n">doctitle</span><span class="p">);</span>
<span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
<span class="nl">font-style</span><span class="p">:</span> <span class="nb">italic</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">@top-right</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">datestamp</span><span class="p">);</span>
<span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">@bottom-right</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="s1">"Page "</span> <span class="n">counter</span><span class="p">(</span><span class="n">page</span><span class="p">);</span>
<span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">@bottom-left</span> <span class="p">{</span>
<span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">guideName</span><span class="p">);</span>
<span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>You’ll see some other items in there such as <code class="language-plaintext highlighter-rouge">prince-script</code>. This means we’re using JavaScript to run some functions to dynamically generate that content. These JavaScript functions are located in the _includes/head_print.html:</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o"><</span><span class="nx">script</span><span class="o">></span>
<span class="nx">Prince</span><span class="p">.</span><span class="nx">addScriptFunc</span><span class="p">(</span><span class="dl">"</span><span class="s2">datestamp</span><span class="dl">"</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="dl">"</span><span class="s2">PDF last generated: July 27, 2021</span><span class="dl">"</span><span class="p">;</span>
<span class="p">});</span>
<span class="o"><</span><span class="sr">/script</span><span class="err">>
</span>
<span class="o"><</span><span class="nx">script</span><span class="o">></span>
<span class="nx">Prince</span><span class="p">.</span><span class="nx">addScriptFunc</span><span class="p">(</span><span class="dl">"</span><span class="s2">guideName</span><span class="dl">"</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="dl">"</span><span class="s2"> User Guide</span><span class="dl">"</span><span class="p">;</span>
<span class="p">});</span>
<span class="o"><</span><span class="sr">/script</span><span class="err">>
</span></code></pre></div></div>
<p>There are a couple of Prince functions that are default functions from Prince. This gets the heading title of the page:</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nx">content</span><span class="p">:</span> <span class="nx">string</span><span class="p">(</span><span class="nx">doctitle</span><span class="p">);</span>
</code></pre></div></div>
<p>This gets the current page:</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nx">content</span><span class="p">:</span> <span class="dl">"</span><span class="s2">Page </span><span class="dl">"</span> <span class="nx">counter</span><span class="p">(</span><span class="nx">page</span><span class="p">);</span>
</code></pre></div></div>
<p>Because the theme uses JavaScript in the CSS, you have to add the <code class="language-plaintext highlighter-rouge">--javascript</code> tag in the Prince command (detailed later on this page).</p>
<h2 id="5-customize-and-run-the-pdf-script">5. Customize and run the PDF script</h2>
<p>Duplicate the pdf-mydoc.sh file in the root directory and customize it for your specific configuration files.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>echo 'Killing all Jekyll instances'
kill -9 $(ps aux | grep '[j]ekyll' | awk '{print $2}')
clear
echo "Building PDF-friendly HTML site for Mydoc ...";
jekyll serve --detach --config _config.yml,pdfconfigs/config_mydoc_pdf.yml;
echo "done";
echo "Building the PDF ...";
prince --javascript --input-list=_site/pdfconfigs/prince-list.txt -o pdf/mydoc.pdf;
echo "done";
</code></pre></div></div>
<p>Note that the first part kills all Jekyll instances. This way you won’t try to serve Jekyll at a port that is already occupied.</p>
<p>The <code class="language-plaintext highlighter-rouge">jekyll serve</code> command serves up the HTML-friendly PDF configurations for our two projects. This web version is where Prince will go to get its content.</p>
<p>The prince script issues a command to the Prince utility. JavaScript is enabled (<code class="language-plaintext highlighter-rouge">--javascript</code>), and we tell it exactly where to find the list of files (<code class="language-plaintext highlighter-rouge">--input-list</code>) — just point to the prince-list.txt file. Then we tell it where and what to output (<code class="language-plaintext highlighter-rouge">-o</code>).</p>
<p>Make sure that the path to the prince-list.txt is correct. For the output directory, I like to output the PDF file into my project’s source (into the files folder). Then when I build the web output, the PDF is included and something I can refer to.</p>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>::</b> You might not want to include the PDF in your project files, since you’re likely committing the PDF to Github and as a result saving the changes from one PDF version to another with each save.</div>
<h2 id="6-add-conditions-for-your-new-builds-in-the-pdf-config-file">6. Add conditions for your new builds in the PDF config file</h2>
<p>In the PDF configuration file, there’s a section that looks like this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>{% if site.product == "mydoc" %}
pdf_sidebar: product2_sidebar
{% endif %}
</code></pre></div></div>
<p>Point to the sidebar you want here.</p>
<p>What this does is allow the prince-list.txt and toc.html files to iterate through the right sidebar. Otherwise, you would need to create a unique prince-list.txt and toc.html file for each separate PDF output you have.</p>
<h2 id="7-add-a-download-button-for-the-pdf">7. Add a download button for the PDF</h2>
<p>You can add a download button for your PDF using some Bootstrap button code:</p>
<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><a</span> <span class="na">target=</span><span class="s">"_blank"</span> <span class="na">rel=</span><span class="s">"noopener"</span> <span class="na">class=</span><span class="s">"noCrossRef"</span> <span class="na">href=</span><span class="s">"/pdf/mydoc.pdf"</span><span class="nt">><button</span> <span class="na">type=</span><span class="s">"button"</span> <span class="na">class=</span><span class="s">"btn btn-default"</span> <span class="na">aria-label=</span><span class="s">"Left Align"</span><span class="nt">><span</span> <span class="na">class=</span><span class="s">"glyphicon glyphicon-download-alt"</span> <span class="na">aria-hidden=</span><span class="s">"true"</span><span class="nt">></span></span> PDF Download<span class="nt"></button></a></span>
</code></pre></div></div>
<p>Here’s what that looks like:</p>
<p><a target=”_blank” class=”noCrossRef” href={{ “pdf/mydoc.pdf”}}”><button type="button" class="btn btn-default" aria-label="Left Align"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> PDF Download</button></a></p>
<h2 id="javascript-conflicts">JavaScript conflicts</h2>
<p>If you have JavaScript on any of your pages, Prince will note errors in Terminal like this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>error: TypeError: value is not an object
</code></pre></div></div>
<p>However, the PDF will still build.</p>
<p>You need to conditionalize out any JavaScript from your PDF web output before building your PDFs. Make sure that the PDF configuration files have the <code class="language-plaintext highlighter-rouge">output: pdf</code> property.</p>
<p>Then surround the JavaScript with conditional tags like this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>{% raw %}{% unless site.output == "pdf" %}
javascript content here ...
{% endunless %}
</code></pre></div></div>
<p>For more detail about using <code class="language-plaintext highlighter-rouge">unless</code> in conditional logic, see [Conditional logic][mydoc_conditional_logic]. What this code means is “run this code unless this value is the case.”</p>
<h2 id="overriding-bootstrap-print-styles">Overriding Bootstrap Print Styles</h2>
<p>The theme relies on Bootstrap’s CSS for styling. However, for print media, Bootstrap applies the following style:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>@media print{*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}
</code></pre></div></div>
<p>This is minified, but basically the <code class="language-plaintext highlighter-rouge">*</code> (asterisk) means select all, and applied the color #000 (black). As a result, the Bootstrap style strips out all color from the PDF (for Bootstrap elements).</p>
<p>This is problematic for code snippets that have syntax highlighting. I decided to remove this de-coloring from the print output. I commented out the Bootstrap style:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>@media print{*,:after,:before{/*color:#000!important;*/text-shadow:none!important;/*background:0 0!important*/;-webkit-box-shadow:none!important;box-shadow:none!important}
</code></pre></div></div>
<p>If you update Bootrap, make sure you make this edit. (Sorry, admittedly I couldn’t figure out how to simply overwrite the <code class="language-plaintext highlighter-rouge">*</code> selector with a later style.)</p>
<p>I did, however, remove the color from the alerts and lighten the background shading for <code class="language-plaintext highlighter-rouge">pre</code> elements. The printstyles.css has this setting.</p>
<div class="tags">
<b>Tags: </b>
<a href="tag_publishing.html" class="btn btn-default navbar-btn cursorNorm" role="button">publishing</a>
<a href="tag_single_sourcing.html" class="btn btn-default navbar-btn cursorNorm" role="button">single_sourcing</a>
<a href="tag_content_types.html" class="btn btn-default navbar-btn cursorNorm" role="button">content_types</a>
</div>
</div>
<hr class="shaded"/>
<footer>
<div class="row">
<div class="col-lg-12 footer">
©2021 IB-Ruby. All rights reserved. <br />
<span>Page last updated:</span> July 3, 2016<br/> Site last generated: Jul 27, 2021 <br />
<p><img src="images/company_logo.png" alt="Company logo"/></p>
</div>
</div>
</footer>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
<!-- /#main -->
</div>
</body>
</html>