Situation
- A user can create a product with text & images
- After creation it can be viewed or downloaded as pdf
I’m trying to get the pdf creation to work.
After looking at different packages pascoual:pdfjs seems to be a good choice for this.
With this I can wrap any template in a canvas and convert it to a pdf. At least thats my understanding of it.
Problem
I’m stumbling with the implementation.
The short “Quick Start”-Guide on the package-page is not very clear to me.
Steps taken
So far I’ve tried to implement it like this:
My template:
<template name="products_detail">
<canvas id="pdfcanvas">
[content of my template]
</canvas>
</template>
My javascript:
Template.products_detail.onRendered(function() {
// Set worker URL to package assets
PDFJS.workerSrc = '/packages/pascoual_pdfjs/build/pdf.worker.js';
// Create PDF
PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
// Fetch the first page
pdf.getPage(1).then(function getPageHelloWorld(page) {
var scale = 1;
var viewport = page.getViewport(scale);
// Prepare canvas using PDF page dimensions
var canvas = document.getElementById('pdfcanvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
page.render({canvasContext: context, viewport: viewport}).promise.then(function () {
});
});
});
});
I pretty much just copy and pasted the javascript from the “Quick Start”-Guide.
Is there more configuration requiered on my end?
Has anyone experience with this?
Any kind of help is much appreciated.
Apart from that
Overall I found that there is a complete lack of Meteor -> PDF tutorials. (At least I couldn’t find any)
So I decided to just write a beginner-friendly one, once I’ve got this to work. Hope this helps other new meteorites out in the future.