Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Lera Ramirez committed May 27, 2021
0 parents commit 8adef93
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 Manuel Lera-Ramirez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 11 additions & 0 deletions printSvg2Pdf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

# Path in MacOS
chrome_executable="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

# Path to the template.html file in this folder
template_file="path/to/template.html"

sed "s/__targetfile__/${1}/" "$template_file" > printSvg2Pdf_temp.html
"$chrome_executable" --headless --disable-gpu --print-to-pdf-no-header --print-to-pdf=$2 printSvg2Pdf_temp.html
rm printSvg2Pdf_temp.html
23 changes: 23 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# PrintSvg2Pdf

A simple bash script to print an svg to pdf without extra margins using headless chrome.

## How to use
In the file `printSvg2Pdf.sh` set the following variables:

* `chrome_executable` to point to the path of your chrome executable.
* `template_file` to point to the path of the file `template.html`, included into the repository.

Then simply:

```
bash printSvg2Pdf.sh input_file.svg output_file.pdf
```

## What it does

`template.html` with an object tag where your image will be referenced in an `object` tag. It has some css to remove all margins, and with javascript it sets the style attribute `@page` to fit exactly the size of the image.

## Acknowledgements

This comes from [this inkscape forum question](https://inkscape.org/forums/questions/exporting-svg-with-linkedembedded-images-to-pdf-without-rasterising/#c28675), and from [this answer](https://stackoverflow.com/a/52128129/5622322) on StackOverflow.
35 changes: 35 additions & 0 deletions template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<head>
<style id='other'>
html,
body {
width: fit-content;
height: fit-content;
margin: 0px;
padding: 0px;

}
</style>
<!-- Tag to append the style -->
<style id=page_style>
</style>

</head>

<body id="test">
<object data="__targetfile__">
</body>

<script>
window.addEventListener("load", fixpage);

function fixpage() {
console.log('ran')
let renderBlock = document.getElementsByTagName("body")[0];
let renderBlockInfo = window.getComputedStyle(renderBlock);
// fix chrome page bug
fixHeight = parseInt(renderBlockInfo.height) + 1 + "px";
document.getElementById("page_style").innerHTML = `@page { size: ${renderBlockInfo.width} ${fixHeight}; margin : 0px } `


}
</script>

0 comments on commit 8adef93

Please sign in to comment.