Hey guys,
I want to implement pdf preview and i have to name the pdf from api. How can I get a preview on Behalf of the name?
Hey guys,
I want to implement pdf preview and i have to name the pdf from api. How can I get a preview on Behalf of the name?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 2, 2024, 12:06 AM EST
To implement a PDF preview in Angular, you can use the
pdf.jslibrary. Here’s a basic example of how you can do it:import * as PDFJS from 'pdfjs-dist';Assuming you have a function
getPdfUrl()that returns the URL of the PDF from your API, you can use the following code to fetch the PDF and display it:let loadingTask = PDFJS.getDocument(this.getPdfUrl()); loadingTask.promise.then(pdf => { console.log('PDF loaded'); // Fetch the first page let pageNumber = 1; pdf.getPage(pageNumber).then(page => { console.log('Page loaded'); let scale = 1.5; let viewport = page.getViewport({scale: scale}); // Prepare canvas using PDF page dimensions let canvas = document.getElementById('the-canvas'); let context = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; // Render PDF page into canvas context let renderContext = { canvasContext: context, viewport: viewport }; let renderTask = page.render(renderContext); renderTask.promise.then(() => { console.log('Page rendered'); }); }); }, function (reason) { // PDF loading error console.error(reason); });In this example, replace
'the-canvas'with the id of your canvas element. This code will fetch the PDF from the API, load it, and render the first page to a canvas element.Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 3, 2024, 4:14 AM EST
I need more information about your setup and goals:
1. Programming Language and Framework:
2. API Details:
3. Naming Mechanism: