New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

svg-paper

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svg-paper

The world's most maintainable way to create paper-printable documents 🖨💘

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
71
increased by162.96%
Maintainers
1
Weekly downloads
 
Created
Source

svg-paper

Travis (.com) npm version npm

The world's most maintainable way to create paper-printable documents 🖨💘

TOC

Workflows

You can print beautiful and maintainable paper documents by following steps:

  1. Design the document with Adobe XD, Figma, or something
  2. Export it as SVG
  3. Embed SVG into your HTML and fix it with svg-paper
  4. That's it 💥

To learn more please see this doc 📝

Installation

CDN

You can get built assets from jsDelivr.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/svg-paper@x.x.x/dist/svg-paper.min.css">

<script src="https://cdn.jsdelivr.net/npm/svg-paper@x.x.x/dist/svg-paper.min.js"></script>

npm

Of course you can install via npm.

$ npm install svg-paper

Basic usage

You can replace or adjust SVG contents in HTML easily with svg-paper like following.

import SvgPaper from 'svg-paper'
// or
// const SvgPaper = require('svg-paper')

const paper = new SvgPaper()

paper
  // replace placeholder to actual value
  .replace('%placeholder1%', 'Actual value 1')
  // ... and more

  // set max width to 1000
  // in the other words, if actual width of the content is bigger than 1000 it shrinks automatically
  .adjustText('#selector1', 1000)

  // set max width to 800 and brings the element #_caseName to the center of 800 width area   
  .adjustText('#selector2', 800, 'middle')

  // of course you can bring it to the end
  .adjustText('#selector3', 800, 'end')

  // automatically wrap or shrink actual content so that it fits within the specified area (600 x 300)
  .adjustTetxarea('#selector4', 600, 300)

  // you can pass some additional args
  .adjustTextarea('#selector5',
    600,  // width 
    300,  // height
    1.2,  // lineHeight : default is 1.2 times font-size
    0.5,  // paddingX   : default is 0.5 times font-size
    0.5,  // paddingY   : default is 0.5 times font-size
    false // nowrap     : default is false. if true, content will not be wrapped
  )

  // finally, apply all replacing and adjusting to DOM
  .apply()

To beautify preview screen, you should add only 3 lines to your HTML 👍

<head>
  ...
  <link rel="stylesheet" href="svg-paper.min.css"> <!-- here -->
  <style>@page { size: A4 }</style> <!-- here -->
</head>

<body class="A4"> <!-- here -->
  <svg>...</svg>
</body>

Just load svg-paper.min.css (or svg-paper.css), in <head> set @page size, and set the class of <body> to specify page size.

Available page sizes are:

  • A3 A3 landscape
  • A4 A4 landscape
  • A5 A5 landscape
  • letter letter landscape
  • legal legal landscape

To learn more about usage please see this doc or some test codes.

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>
  <svg>...</svg>
</body>
paper.apply()

document.querySelector('#blinder').style.display = 'none'

With non Node.js back-end

Even if your back-end isn't Node.js, of course you can use svg-paper 👍

The most easy ways is just passing replacements and text/textarea adjustment parameters to front-end as JSON string.

e.g. PHP and Twig

// Controller
public function paper(YourModel $model, YourPaperDefinition $paper)
{
    return $this->render('paper.html.twig', [
        'svg' => $paper->getSvg(),
        'replacements' => $paper->getReplacements($model),
        'textAdjustments' => $paper->getTextAdjustments(),
        'textAreaAdjustments' => $paper->getTextAreaAdjustments(),
    ]);
}
// YourPaperDefinition
class YourPaperDefinition
{
    public function getSvg()
    {
        return file_get_contents('/path/to/paper.svg');
    }
    
    public function getReplacements(YourModel $model): array
    {
        return [
            '%placeholder1%' => 'Actual value 1',
            // ... and more
        ];
    }
    
    public function getTextAdjustments(): array
    {
        return [
            // selector => [args for SvgPaper.adjustText()]
            '#selector1' => [1000],
            '#selector2' => [800, 'middle'],
            '#selector3' => [800, 'end'],
        ];
    }

    public function getTextareaAdjustments(): array
    {
        // selector => [args for SvgPaper.adjustTextarea()]
        return [
            '#selector2' => [600, 300], 
            // ... and more
        ];
    }
}
{# paper.html.twig #}
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="svg-paper.min.css">
  <style>@page { size: A4 }</style>
</head>

<body class="A4">
  {{ svg|raw }}
  <div data-replacements="{{ replacements|json_encode }}"></div>
  <div data-text-adjustments="{{ textAdjustments|json_encode }}"></div>
  <div data-textarea-adjustments="{{ textAdjustments|json_encode }}"></div>
  <script src="svg-paper.min.js"></script>
  <script src="your-script.js"></script>
</body>
</html>
// your-script.js
const replacements = $('[data-replacements]').data('replacements')
const textAdjustments = $('[data-adjustments]').data('text-adjustments')
const textareaAdjustments = $('[data-adjustments]').data('textarea-adjustments')

const paper = new SvgPaper()

for (let [search, replacement] of Object.entries(replacements)) {
  paper.replace(search, replacement)
}

for (let [selector, args] of Object.entries(textAdjustments)) {
  paper.adjustText(selector, ...args)
}

for (let [selector, args] of Object.entries(textareaAdjustments)) {
  paper.adjustTextarea(selector, ...args)
}

paper.apply()

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! ✨

Keywords

FAQs

Package last updated on 13 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc