Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@votingworks/ballot-renderer
Advanced tools
Renders ballots to PDF.
import { getContests, vote, CompletedBallot } from '@votingworks/ballot-encoder'
import render from '@votingworks/ballot-renderer'
// build the ballot
function makeBallot(): CompletedBallot {
const ballotStyle = election.ballotStyles[0]
const precinct = election.precincts[0]
const ballotId = 'abcde'
const contests = getContests({ ballotStyle, election })
const votes = vote(contests, {
'judicial-robert-demergue': 'yes',
'judicial-elmer-hull': 'yes',
'question-a': 'yes',
'question-b': 'no',
'question-c': 'yes',
'proposition-1': 'yes',
'measure-101': 'no',
'102': 'yes',
})
return {
election,
ballotId,
ballotStyle,
precinct,
votes,
}
}
// render to file (NodeJS)
import * as fs from 'fs'
import { promisify } from 'util'
const writeFile = promisify(fs.writeFile)
async function renderToFile(
path: string,
ballot: CompletedBallot
): Promise<void> {
await fs.writeFile('output.pdf', await render(ballot))
}
// render inline in the browser (React)
import * as ReactDOM from 'react-dom'
import * as b64 from 'base64-js'
async function main() {
ReactDOM.render(
<iframe
src={`data:application/pdf;base64,${b64.fromByteArray(
await render(ballot)
)}`}
title="ballot"
style={{ height: '100vh', width: '100%' }}
/>,
document.body
)
}
Rendering a ballot is as easy as passing a CompletedBallot
to render
,
returning a Buffer
:
import { render } from '@votingworks/ballot-renderer'
async function renderAndDoSomethingWithPDF(
ballot: CompletedBallot
): Promise<void> {
const pdfContents = render(ballot)
// …
}
By default this will use the latest encoding from @votingworks/ballot-encoder
,
but if you wish you can specify the version to use:
import { EncoderVersion } from '@votingworks/ballot-encoder'
import { render } from '@votingworks/ballot-renderer'
async function renderAndDoSomethingWithPDF(
ballot: CompletedBallot
): Promise<void> {
const pdfContents = render(ballot, { encoderVersion: EncoderVersion.v0 })
// …
}
Sometimes it's useful to be able to separate the ballot document creation step from the PDF rendering step. For example, you may wish to generate the ballot document in a client and send it to a server for rendering. Here's how you can achieve that:
// client (e.g. a browser)
import ballotToDocument from '@votingworks/ballot-renderer/renderers/ballotToDocument'
async function renderPDF(): Promise<Blob> {
const response = await fetch('http://localhost:8080/my/render/endpoint', {
method: 'POST',
body: JSON.stringify(ballotToDocument(completedBallot)),
})
return response.blob()
}
// server (e.g. nodejs + express)
import documentToPdf from '@votingworks/ballot-renderer/renderers/documentToPdf'
import express from 'express'
const app = express()
app.post('/my/render/endpoint', async (req, res) => {
const { body } = req
const json = new TextDecoder().decode(body)
const document = JSON.parse(json)
const pdf = await documentToPdf(document)
res.end(pdf)
})
app.listen(8080)
See the renderers folder for all the partial renderings you can do.
FAQs
Renders ballots to PDF.
The npm package @votingworks/ballot-renderer receives a total of 0 weekly downloads. As such, @votingworks/ballot-renderer popularity was classified as not popular.
We found that @votingworks/ballot-renderer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.