svg-paper
![](https://data.jsdelivr.com/v1/package/npm/svg-paper/badge)
The world's most maintainable way to create paper-printable documents 🖨💘
![](https://user-images.githubusercontent.com/4360663/121766151-f6b64d80-cb8a-11eb-8736-3a28b4c03d70.png)
TOC
Workflows
You can print beautiful and maintainable paper documents by following steps.
- Design the document with Adobe XD, Figma, or something
- Export it as SVG
- Embed SVG into your HTML and fix it with svg-paper on client side
- That's it 💥
Installation
CDN
You can get the built assets from jsDelivr.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/svg-paper/dist/svg-paper.min.css">
<script src="https://cdn.jsdelivr.net/npm/svg-paper/dist/svg-paper.min.js"></script>
npm
Of course you also can install via npm.
$ npm install svg-paper
How to prepare SVG template
See this doc 📝
Basic usage
First, just embed SVG content in .paper
element like following.
<body>
<div class="paper">
<svg>...</svg>
</div>
</body>
Next, load svg-paper[.min].js
with <script>
tag or import/require svg-pager[.min].js
as a module.
<script src="svg-paper.min.js"></script>
<script>
const paper = new SvgPaper()
</script>
or
import SvgPaper from 'svg-paper'
const paper = new SvgPaper()
Then you can replace or adjust SVG contents in DOM easily with svg-paper like following.
paper
.replace('%placeholder1%', 'Actual value 1')
.adjustText('#selector1', 1000)
.adjustText('#selector2', 800, 'middle')
.adjustText('#selector3', 800, 'end')
.adjustTetxarea('#selector4', 600, 300)
.adjustTextarea('#selector5',
600,
300,
1.2,
0.5,
0.5,
false
)
.apply()
Beautify preview screen
To beautify preview screen, you should add only 3 lines to your HTML 👍
<head>
...
<link rel="stylesheet" href="svg-paper.min.css">
<style>@page { size: A4 }</style>
</head>
<body>
<div class="paper A4">
<svg>...</svg>
</div>
</body>
Just load svg-paper.min.css
(or svg-paper.css
), in <head>
set @page size, and set the class of .paper
element to specify page size.
Available page sizes are:
A3
A3 landscape
A4
A4 landscape
A5
A5 landscape
letter
letter landscape
legal
legal landscape
Passing variables from back-end to front-end
svg-paper depends on DOM, so in most cases you have to pass variables to be replaced with placeholders in template from back-end to front-end.
The most easy ways is just passing replacements and text/textarea adjustment parameters to front-end as JSON string.
PHP and Twig example
public function paperAction($id)
{
$model = $repository->findById($id);
return $this->render('paper.html.twig', [
'svg' => file_get_contents('/path/to/paper.svg'),
'replacements' => [
'%name%' => $model->name,
// ... and more
],
]);
}
{# paper.html.twig #}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="svg-paper.min.css">
<style>@page { size: A4 }</style>
</head>
<body>
<div class="paper A4">
{{ svg|raw }}
</div>
<div data-replacements="{{ replacements|json_encode }}"></div>
<script src="svg-paper.min.js"></script>
<script src="your-script.js"></script>
</body>
</html>
const paper = new SvgPaper()
const replacements = $('[data-replacements]').data('replacements')
for (let [search, replacement] of Object.entries(replacements)) {
paper.replace(search, replacement)
}
paper.apply()
Tips
Hiding content before placeholders are replaced
svg-paper replaces placeholders and adjust text/textarea after DOM loaded, so the content before replaced and adjusted will be shown on the screen for a moment 🤔
This problem is very easy to solve just by adding some "blinder" layer on the content and disappear it after .apply()
👍
<body>
<div id="blinder" style="width:100vw; height:100vh; background-color:#ccc"></div>
<div class="paper">
<svg>...</svg>
</div>
</body>
paper.apply()
document.querySelector('#blinder').style.display = 'none'
PDF generation
You can easily print to PDF directly by using electron-pdf.
$ npm install --global electron-pdf
$ electron-pdf your-document.html your-document.pdf
Enjoy! ✨