Submit a pdf file created with html2pdf to an S3 bucket using form data

Currently, I have the following script:

exportPDF(id) {
const options = {
  filename: 'INV' + id + '.pdf',
  image: { type: 'jpeg', quality: 0.98 },
  html2canvas: {
    scale: 2, dpi: 300,
    letterRendering: true,
    useCORS: true
  },
  jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
}
var element = document.getElementById('export-content');
var d = html2pdf().from(element).set(options).save();

const formData: FormData = new FormData();
formData.append('file', d);

// Service for uploading file to s3 bucket
this._uploadService.upload('invoice', formData).subscribe(res => {
  console.log(res);
}, (error: any) => { 
  console.log(error, 'error');
});}

I need assistance in storing the generated pdf file to an S3 bucket and then sending it as an email attachment from S3 without storing any blob data in the database. I just want to store the S3 URL in the database. Any help would be appreciated.

While I found a solution by converting the blob string to a file, I am facing issues with storing the response string in a global variable for conversion and sending via an API call.

this.blobString: any;
html2pdf().from(element).set(options).toPdf().output('datauristring').then(function (res) {
  this.blobString = res;

 // Service for uploading file to s3 bucket
 this._uploadService.upload('invoice', this.blobString).subscribe(res => {
   console.log(res);
 }, (error: any) => { 
   console.log(error, 'error');
 });

});
console.log(this.blobString);

When checking the console, I see "undefined." How can I set Worker data globally?

Answer №1

If you're dealing with promises, make sure your console.log waits for the promise response before triggering. Adjust your code to this:

let blobString: any;
html2pdf().from(element).set(options).toPdf().output('datauristring').then(function (res) {
  blobString = res;
  console.log(blobString);
});

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

Can a standard tuple be matched with its corresponding key?

This code snippet showcases a function that can recognize that the key "banana" cannot have the value "red": type Fruits = { banana: 'yellow' | 'green' strawberry: 'red' } const fruit = <K extends keyof Fruits>(modu ...

What is the method for selecting the desired month on a primeng calendar with multiple selection enabled?

I am looking for a solution to make my inline primeNg Calendar display the month of a specific date in the model, and when I remove dates from the model, I want it to show the current month. I have tried using defaultDate={{value}} and minDate={{value}}, a ...

`When the component is loaded, subscribing to the event will work properly.`

I am facing challenges with handling data retrieved from a Database. I want to send it to a central service so that I can utilize it later when loading the components that require this data. The issue I'm encountering is that the central service appea ...

The NodeJS backend is receiving an empty request body from the Ionic frontend application

In my Ionic app (Angular2) with a Node backend, I have implemented a feature called Level, where each Level has its own title. Users can view a list of levels and add new ones through a modal. However, I encountered an issue where the titles are not being ...

Initial position of the range slider in IONIC 2

I have been searching the web extensively to find a solution to this particular issue. I am working with a range slider and trying to set its default starting values, but so far, I haven't had any luck. I've checked both the official documentatio ...

What is the best approach to limit the return type of a function in JSX?

Is it possible to create a function where the return type should be a specific JSX type? For instance: const setHosting : <GitlabLogo> | <GithubLogo> = (h: Hosting) => ??? In this case, the return type must either be <GitlabLogo> or ...

Create a custom button in Material-UI using Styled-components, and integrate it with React

I'm currently working on a project using React, TypeScript, and styled components along with the material-ui library. I have created styled material-ui buttons as shown below: import React from 'react' import styled from 'styled-compone ...

The specified object '[object Object]' is not compatible with NgFor, which only supports binding to iterable data types like Arrays

I have some code that is attempting to access objects within objects in a template. <table class="table table-striped"> <tr *ngFor="let response of response"> <td><b>ID</b><br>{{ response.us ...

Angular 5 error: Decorators do not support function expressions

I am struggling to compile my angular project using the command ng build --prod The issue arises in the IndexComponent : index.componenent.ts import { Component, OnInit } from '@angular/core'; import { indexTransition } from './index.anim ...

Ensuring the completion of all validations in Angular 4

Currently, I'm working on a project that involves 3 FormControls. I am in need of a validation process that ensures either all three have values selected or none at all. Is there a method to achieve this type of validation? ...

The issue is that TypeScript is indicating that the type 'string | string[]' cannot be assigned to the type 'string'

I recently upgraded to Angular 16 and encountered an issue with an @Input() property of type string | string[]. Prior to the upgrade, everything was functioning correctly, but now I am experiencing errors. I am uncertain about where I may have gone wrong i ...

Error: The terminal reports that the property 'then' cannot be found on the data type 'false' while trying to compile an Angular application

In my Angular application, which I initiate through the terminal with the command ng serve, I am encountering build errors that are showing in red on the terminal screen. ✔ Compiled successfully. ⠋ Generating browser application bundles... Error: s ...

Tips for saving metadata about properties within a class

I am looking to add metadata to properties within classes, specifically using abbreviations for property names. By using annotations like @shortName(abbreviated), you can label each property as follows: function shortName(shortName: string){ return fu ...

What are some ways I can enhance the typography within Material UI?

Currently, I am in the process of developing a custom theme utilizing createMuiTheme. However, my application requires more typography variants than what Material UI provides out of the box. I need to extend the typography so that it aligns with my specifi ...

Encountering a 401 error when trying to access OneNote resource in Angular 5 with Microsoft Graph

I have encountered an issue while trying to request resources from Microsoft graph (for OneNote API). My concern revolves around the correct method of obtaining an access token. I am utilizing the implicit flow for my Angular 5 frontend application. The p ...

Unable to initiate a new project in Node.js

As I was working on adding a new project in Angular, everything was running smoothly until today. However, when trying to create a new project today, I noticed that the node_modules folder is missing and encountered the following errors: https://i.stack.i ...

Update an API call to switch from promises to observables, implementing axios

Recently, I have been experimenting with using rxjs for API requests in a React application and this is the approach that I have come up with. What are your thoughts on this method? Are there any best practices that you would recommend following? If you ...

My goal is to monitor every action (button) that users take while using the PDF viewer

Each browser displays the view differently, and I am interested in monitoring specific user actions such as button presses on a PDF viewer. I am currently setting up an iframe and configuring its attributes. Is there a way to achieve this? ...

Discovering how to properly run tests on a function within an if statement using Jasmin, Karma

I've recently started unit testing and I'm facing an issue with my component. The component calls a service to display a list of students. getListStudents() { this.noteService.getStudents({}).subscribe(res => { this.students= res })} Then ...

Best practices for utilizing ngrx/store in Angular 2

As I am refactoring my Angular 2 applications to follow the ngrx pattern, some questions have arisen in my mind: My application retrieves a list of apps and a list of app categories. Can I manage state like "selectedCategory" (where only one can be select ...