Home >Web Front-end >JS Tutorial >Transforming Starlight into PDF: experience and insights

Transforming Starlight into PDF: experience and insights

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-15 10:24:42659browse

Imagine you are given a task: create a new documentation website in a week. It should be visually appealing, fast, and easy to navigate. You’re handed a pile of *.docs files, images, and screenshots, with the instruction to "get it done".

There are many excellent tools to choose from, such as Docusaurus, Nextra, VitePress, Docus, and others. Previously, I had a great experience building a documentation website with Starlight, so it was my choice for this task. However, I discovered a missing feature: the ability to generate a PDF from the documentation. And it was one of the requirements. "Sounds like a nice side project," I thought for myself.

Tackling the task

At first, it seemed straightforward: fetch the pages, parse the HTML, group the content, and voila!

Starlight powered websites have a Next button to navigate through the documentation. As PDF essentially is an array of pages, it seemed logical to parse them one by one, using this Next button. Since the website generates static pages, I quickly wrote a script to fetch the HTML, query the necessary parts, and combine everything together. However, generating a PDF that retained the website's styles proved to be more complex. After some brainstorming, I realized Puppeteer was the best solution.

Now the process became clear:

  1. Identify the starting page. This is the first page with a Next button.
  2. Navigate through the pages. Extract the heading and main content from each page and at the same time build a table of contents.
  3. Combine the content. Add page breaks and additional styles.
  4. Prepare the final HTML. Rewrite the of the initial page with the resulting HTML.
  5. Load resources. Scroll the page to the bottom to load all the images.
  6. Generate the PDF. Puppeteer's Page.pdf() method nails it.
  7. Done!

This is how starlight-to-pdf works. Following this pattern, you can build similar tools for other documentation frameworks lacking PDF export functionality.

Next steps

Once the basic functionality was ready, it was time to add some extras. Below are the most interesting and challenging features.

Adding headers and footers

It's nice to have a page number and some additional information in the header or footer. Puppeteer's Page.pdf() method accepts headerTemplate and footerTemplate options. These options accept HTML strings. Puppeteer automatically injects values into the elements that have specific utility classes:

  • .date: formatted date;
  • .title: web page's tag value;</li> <li> .url: page's URL on which printing function was called;</li> <li> .pageNumber: current page number;</li> <li> .totalPages: total number of pages in the document.</li> </ul> <p>As we combine all the content on one page before printing, title and url don't have much value for us: the inserted value will always remain the same. However, other classes help a lot. Here’s an example footer template:<br> </p> <pre class="brush:php;toolbar:false"><style> .footer-container { --color: #000; display: flex; align-items: center; justify-content: space-between; border-block-start: 1px solid var(--color); color: var(--color); font-size: 10px; font-family: Arial, Helvetica, sans-serif; margin-inline: 1.5cm 1cm; padding-block: 0.25cm 0.5cm; width: 100%; } </style> <div> <p>To use this, do not forget to set the displayHeaderFooter property to true:<br> </p> <pre class="brush:php;toolbar:false">import puppeteer from 'puppeteer'; const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://someUrl'); const footerTemplateStr = '<style>...<style><div>...</div>' // replace with the HTML string from the example above await page.pdf({ displayHeaderFooter: true, footerTemplate: footerTemplateStr }) </pre> <p>Here are some findings that you should keep in mind:</p> </pre> <ul> <li>The template must be a valid HTML structure.</li> <li>Define font-size CSS property as Puppeteer's default value is 0.</li> <li>Use inline <style> tag to define your styles. Website styles are not available inside the templates.</li> <li>Images should be encoded as base64 strings.</li> <li>Use Puppeteer's margin option to achieve the desired layout.</li> </ul> <h3> What about CLI styles? </h3> <p>Everything works fine, the resulting PDF looks great, but the terminal messages feel bland. Attention to details separates the good from the great, isn’t it? Let's make our messages more colorful and easier to read.</p> <p>Here comes the magic of ANSI escape sequences. I decided that 4-bit colours would be enough for the job. Let's say you want to have a white text on red background <em>(that's what I used for my [ERROR]: prefix before error messages)</em>. Here is how you can achieve this look:<br> </p> <pre class="brush:php;toolbar:false">console.log('\x1b[37;41m', 'White on red message'); </pre> <p>Let's break it down:</p> <ul> <li> x1b[ is a hexadecimal escape code (you may also use u001b as the Unicode alternative);</li> <li> 37 is a foreground white color, where 3 stands for foreground and 7 for the white color;</li> <li> 41 is a background red color, where 4 stands for background and 1 for the red color.</li> </ul> <p>Everything is working, but now all of our console.log() output will be styled in this manner. To reset the style back to default, simply add the reset sequence x1b[0m at the end:<br> </p> <pre class="brush:php;toolbar:false">console.log('\x1b[37;41m', 'White on red message', '\x1b[0m'); </pre> <p>Much better. What if we want bold cyan text on a gray background <em>(bright black in the names of 4-bit colors)</em>? It's easy:<br> </p> <pre class="brush:php;toolbar:false">console.log('\x1b[1;36;100m', 'Cyan on gray message in bold', '\x1b[0m'); </pre> <p>Here’s what each part does:</p> <ul> <li> 1 after the escape code applies the bold effect;</li> <li> 36 sets the text color to cyan;</li> <li> 100 changes the background to bright black color, where 10 means <em>bright</em> and 0 is a code for <em>black</em>.</li> </ul> <p>Using this knowledge, you can make your CLI tool visually appealing. For example, I styled all URLs and file paths as underlined blue text in my project:<br> </p> <pre class="brush:php;toolbar:false">console.log('\x1b[4;34m', './underlined/blue', '\x1b[0m') </pre> <p>Check out this cheatsheet to learn more on the topic.</p> <h2> Wrapping up </h2> <p>You never know when a routine task might inspire a rewarding side project. Development of starlight-to-pdf provided valuable experience with Puppeteer and CLI styling, and a new tool emerged in the open source community. Here’s a quick demonstration:</p> <p><img src="https://img.php.cn/upload/article/000/000/000/173690789360353.jpg" alt="Transforming Starlight into PDF: experience and insights"></p> <p>The above is the detailed content of Transforming Starlight into PDF: experience and insights. For more information, please follow other related articles on the PHP Chinese website!</p></div><div class="nphpQianMsg"><a href="javascript:void(0);">css</a> <a href="javascript:void(0);">html</a> <a href="javascript:void(0);">Static</a> <a href="javascript:void(0);">Array</a> <a href="javascript:void(0);">define</a> <a href="javascript:void(0);">if</a> <a href="javascript:void(0);">for</a> <a href="javascript:void(0);">date</a> <a href="javascript:void(0);">Error</a> <a href="javascript:void(0);">break</a> <a href="javascript:void(0);">using</a> <a href="javascript:void(0);">Property</a> <a href="javascript:void(0);">console</a> <a href="javascript:void(0);">number</a> <a href="javascript:void(0);">function</a> <a href="javascript:void(0);">default</a> <a href="javascript:void(0);">this</a> <a href="javascript:void(0);">margin</a> <a href="javascript:void(0);">background</a> <a href="javascript:void(0);">table</a> <a href="javascript:void(0);">everything</a> <a href="javascript:void(0);">Other</a><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="Using LRU Cache in Node.js and TypeScript" href="https://m.php.cn/faq/1796753817.html">Using LRU Cache in Node.js and TypeScript</a></span><span>Next article:<a class="dBlack" title="Using LRU Cache in Node.js and TypeScript" href="https://m.php.cn/faq/1796753834.html">Using LRU Cache in Node.js and TypeScript</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1609.html" title="An in-depth analysis of the Bootstrap list group component" class="aBlack">An in-depth analysis of the Bootstrap list group component</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1640.html" title="Detailed explanation of JavaScript function currying" class="aBlack">Detailed explanation of JavaScript function currying</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1949.html" title="Complete example of JS password generation and strength detection (with demo source code download)" class="aBlack">Complete example of JS password generation and strength detection (with demo source code download)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2248.html" title="Angularjs integrates WeChat UI (weui)" class="aBlack">Angularjs integrates WeChat UI (weui)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2351.html" title="How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills" class="aBlack">How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>