Converting HTML to PDF with Angular 2

I'm looking for a solution to convert an HTML template to PDF and download the file in Angular 2. Specifically, I need TypeScript code that can achieve this. Can anyone provide guidance or share some sample code?

Answer №1

I've incorporated an external library into my Angular4 app to facilitate document export: https://github.com/eligrey/FileSaver.js/

While it may not fulfill all the requirements for the entire page, it proved to be ideal for extracting data in CSV / PDF format from my application.

Below is a snippet of the code I implemented to extract the data. I linked this function to a button to initiate the process:

exportDatas(documentType: string) {
  this.MyRandomServiceThatReturnsData.getDatas(MyParameters1, ...)
  .subscribe(result => {
    const blob = new Blob([result.blob()]);
    const objectUrl = URL.createObjectURL(blob);
    FileSaver.saveAs(blob, 'Export.' + documentType);
  });
}

Answer №2

Creating a PDF that mirrors an Angular HTML template with filled-in data can be surprisingly challenging. I often need this for reports, where the layout is designed in HTML and users should be able to view it on-screen or print/save/email it as a PDF.

I initially thought a PDF printer driver would suffice, but it seems to only work for server-side completed pages.

Fortunately, I discovered a reliable solution in the form of a commercial application called HiQPdf. It operates in C# and offers extensive customization options. I consider it a cost-effective choice and regret not finding it sooner, as I wasted over 20 hours attempting to achieve the same functionality on my own.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for retrieving JSON files within karma tests

I am currently working on developing a test for my TypeScript class that involves retrieving data from a JSON file. readData<T>(filePath: string): Promise<T> { return Qajax.getJSON(filePath); } For testing purposes, I am utilizing the Jas ...

Mapping URLs to objects in JavaScript, TypeScript, and Angular4

I have implemented a class called SearchFilter class SearchFilter { constructor(bucket: string, pin: number, qty: number, category: string) { } } When the user performs a search, I populate the filter i ...

Enhance the functionality of MatOption by developing a component that allows users to easily select

To make my life easier, I want to create a shared component that can select or deselect all elements in a mat-select. This way, I won't have to repeatedly implement the same functionality in my app. After considering different options, I came across ...

Tips for implementing HTTP requests in Angular 2 without TypeScript

The demonstrations provided by the angular team only illustrate injecting Http for typescript. https://angular.io/docs/js/latest/api/http/Http-class.html How can this be accomplished in JavaScript? import {Http, HTTP_PROVIDERS} from 'angular2/http& ...

Adding .js extension to relative imports in TypeScript compilation for ES6 modules

This particular issue may appear simple at first glance, but determining the required settings/configurations to resolve it is not straightforward. Below are the directory structure and source code for a Hello World program: Directory Structure: | -- Hel ...

Best Practices for Securing Your Spring Boot RESTful API with OAuth2 and Implementing Social Login

Currently, I am implementing an Angular 2 Front End application to act as a client that will be consuming resources from a Spring RESTful Web Service. In order to secure this web service, I have decided to implement OAuth 2 authentication and Social Login ...

What is the process of combining two objects in TypeScript?

As part of my project, I am using two different get requests. The first request returns data in the form of: department: string[] The second get request provides an object structured like this: globalObj: { count: number[], admin: { department: ...

Developing desktop applications with Angular 2 using NWjs

Looking to create an Angular 2 Desktop App using NWjs. Where can I find the entry point? Could someone provide some examples of developing Angular 2 Desktop Apps with NW.js? ...

Display the Astro component based on the query of the current page's type

I am using Astro, GraphQL (Apollo Client), Typescript and React. Within my dynamic route: [...slug].astro file, I have a requirement to conditionally display a specific Astro component. I was able to achieve this using the following logic: {data.page.ty ...

Navigate to a new tab using this.router.navigate

Is there a way to redirect the user to a specific page with ${id} opening in a new tab, after clicking a button in an angular material dialog box? I want to leave the dialog box open while querying the new page. Currently, the redirect happens but not in a ...

The error message "Cannot send headers after they have already been sent to the client" is caused by attempting to set headers multiple

Although I'm not a backend developer, I do have experience with express and NodeJS. However, my current project involving MongoDB has hit a roadblock that I can't seem to resolve. Despite researching similar questions and answers, none of the sol ...

Vue.js Element UI dialog box

Is there a method to customize the close button in el-dialog and replace it with my own design? For instance, can I change the default close button located at the top left corner of the dialog? <el-dialog title="Tips" :visible.sync=" ...

When using Angular's .navigateByUrl() method, it successfully navigates to the desired page, however the html content is not

Whenever I try to navigate to the home page after logging in, I encounter an issue. I have a navbar <app-header></app-header> with two links - one for signing up and another for logging in. After successfully logging in, my goal is to navigate ...

Embracing the Power of React.StrictMode in Next.js

In my project using Next.js with React, I have React.StrictMode set to true in next.config.mjs. I'm wondering if it's still necessary to wrap my components with <React.StrictMode> in the layout.tsx file. Do both methods serve the same purpo ...

Leveraging Shared Modules Component across multiple modules in Angular can enhance code re

In my project structure, I have a shared folder containing shared.module.ts. Additionally, there is a modules folder with sub-modules, one of which is Dashboard.module.ts. Inside the shared module, I created a custom sidebar menu that I intend to use withi ...

What is the purpose of the .Class method in ng.core.component within ES5?

ng.core.Component({ selector:'myapp', template:'<h1>Hello World</h1>' }). Class( { constructor:function() } ); ...

Customizing Material UI Select for background and focus colors

I am looking to customize the appearance of the select component by changing the background color to "grey", as well as adjusting the label and border colors from blue to a different color when clicking on the select box. Can anyone assist me with this? B ...

Using Moment JS to display the days of the upcoming week

I'm in the process of developing a weather application and I need to create code that will display the upcoming week's weather forecast. The only information I have from the server is a "time" entity with a "value" set for next Monday such as "20 ...

Is it possible to interpret this in TypeScript?

I need assistance in understanding the following Code snippet. Can someone guide me through it? [Symbol.iterator](): IterableIterator<IPv4> { return this; } ...

Error encountered: `npm ERR! code E503`

While attempting to execute npm install on my project, which was cloned from my GitHub repository, I encountered the following error: npm ERR! code E503 npm ERR! 503 Maximum threads for service reached: fs-extra@https://registry.npmjs.org/fs-extra/-/fs-ex ...