Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@pirireis/mapbox-print-pdf
Advanced tools
A library for saving high resolution versions of mapbox-gl maps to a pdf with an optional header and/or footer that scales with the size of the format.
npm install mapbox-print-pdf --save
Example usage for printing a map in A3 format in portrait mode:
var printPdf = require('mapbox-print-pdf');
printPdf.build()
.format('a3')
.portrait() // Unnecessary since it's the default but it's included for clarity.
.print(map, mapboxgl)
.then(function(pdf) {
pdf.save('map.pdf');
});
Example usage for printing a map in a custom format in landscape mode and a dpi of 200:
var printPdf = require('mapbox-print-pdf');
printPdf.build()
.format({width: 1280; height: 980; unit: "pt"; name: "my-format"})
.landscape()
.dpi(200)
.print(map, mapboxgl)
.then(function(pdf) {
pdf.save("map-custom-format.pdf");
})
Example usage for printing a map in A2 format with a footer and a map scale control:
#footer {
display:none;
}
.footer {
width:100%;
height:auto;
overflow:hidden;
margin-top: 10px;
}
.info {
font-size: 18px;
}
.info > span {
font-weight:bold;
}
<div data-scale-height="margin-top" id="footer" class="footer">
<p data-scale-sum="font-size" class="info"><span>Created with</span>: © Mapbox, © OpenStreetMap</p>
<p data-scale-sum="font-size" class="info"><span>Author</span>: Eddie Larsson</p>
</div>
var printPdf = require('mapbox-print-pdf');
var elementClonedCb = function(elem) {
elem.removeAttribute("id");
}
printPdf.build()
.format("a2")
.footer({
html: document.getElementById("footer"),
baseline: {format: "a4", orientation: "p"}
}, elementClonedCb)
.scale({maxWidthPercent: 10, unit: "metric"})
.print(map, mapboxgl)
.then(function(pdf) {
pdf.save("map-with-footer.pdf");
});
The attributes data-scale-height and data-scale-sum are used to allow for scaling of the footer with its content.
One important thing to note is that when specifying a custom format or margins pixels can't be used.
Custom formats can either be supplied when calling format, this is then in the form of an object containg width,height,unit and name of the format. The same form of object can also be added like so:
var printPdf = require('mapbox-print-pdf');
printPdf.formats.addFormat({
width: 500,
height: 500,
unit: "pt"
}, "format-name");
A scale control can be added to the printed map, this is specified with a maximum percentage of the maps width it's allowed to occupy along with the unit to use for the scale.
var printPdf = require('mapbox-print-pdf');
printPdf.build()
.scale({maxWidthPercent: 15, unit: "imperial"})
//...
The supported units can be found here.
At this time the supported units are:
Margins can be specified in any unit except pixels, they are considered absolute and do not scale with the size of the format. They can either be specified as a single number, in which case the same margin is used for all sides, or as an object containing the top, right, bottom and left values.
var printPdf = require('mapbox-print-pdf');
printPdf.build()
.margins(5, "pt")
//...
var printPdf = require('mapbox-print-pdf');
printPdf.build()
.margins({
top: 6,
right: 3,
left: 3,
bottom:6
}, "mm")
//...
A header and/or footer can be specified either by passing in an html string or html object, the latter is usually preferred. In addition to the html itself the baseline format the header/footer was designed for must be provided, this could be a custom format (a name property isn't neccessary) or the name of a registered format. An additional optional property is the orientation of the baseline format (default is portrait). The header/footer height is based on the content height, so specify what it in the normal css.
var printPdf = require('mapbox-print-pdf');
printPdf.build()
.header({
html: '<div><p data-scale-sum="font-size">I\'m a header</p>',
baseline: {format: 'a4', orientation: 'l'}
})
.footer({
html: document.getElementById('footer-template'),
baseline: {
format: {
height: 500,
width: 300,
unit: "pt"
}
}
}, function(elem) {
elem.removeAttribute("id");
})
//...
The supported scaling attributes are:
The properties to scale should be specified as a space separated list of properties in either camel-case or hyphen-separated. The exception to this is the data-scale-handler attribute which should specify the id of a custom scaling function.
Custom scaling functions can be specified in the header/footer object, this should be in the form of an associative array of id: handler. The function should accept two parameters, the element to scale and an object containing information about the scaling. It should return an array of new style values specified as objects with prop and value properties where prop should be the property name in camelCase and value is the new value.
The second argument to the handler function has the following properties:
The dimension objects have the following methods:
<div id="header-template">
<p data-scale-handler="custom-handler">Passed to the handler</p>
</div>
var printPdf = require('mapbox-print-pdf');
var customHandler = function(elem, scalingObj) {
var newStyles = [];
// Calculate new styles.
return newStyles;
}
printPdf.build()
.header({
html: document.getElementById("header-template"),
baseline: "a4",
handlers: {"custom-handler": customHandler}
}, function(elem) {
elem.removeAttribute("id");
})
The available properties/values are restricted by html2canvas, for a list of supported features see html2canvas.
Attribution of maps is required. See tile provider terms for details.
Licensed under the MIT License.
FAQs
A library for printing mapbox maps as pdfs
We found that @pirireis/mapbox-print-pdf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.