How can Observable data be transferred to PDF (pdfMake) in Ionic with Angular?

I am currently utilizing pdfMake to create PDFs from observable data, but I am encountering an issue where the PDF either appears empty or displays [object Object]. Below is the snippet of my code:

downloadPDF() {
  pdfMake.vfs = pdfFonts.pdfMake.vfs;

  var docDefinition = {
    content: [
      {
        text: "lightHorizontalLines:",
        fontSize: 14,
        bold: true,
        margin: [0, 20, 0, 8],
      },
      {
        style: "tableExample",
        table: {
          headerRows: 1,
          body: [
            [{ text: "Details", style: "tableHeader" }],
            [{ text: this.observableData }], // observable to pdf data not showing here.
          ],
        },
        layout: "lightHorizontalLines",
      },
    ],
  };

  this.pdfObj = pdfMake.createPdf(docDefinition);

  if (this.plt.is("cordova")) {
    this.pdfObj.getBase64(async (data) => {
      try {
        let path = `pdf/myletter_${Date.now()}.pdf`;

        const result = await Filesystem.writeFile({
          path,
          data: data,
          directory: FilesystemDirectory.Documents,
          recursive: true,
        });
        this.fileOpener.open(`${result.uri}`, "application/pdf");
      } catch (error) {
        console.log("Unable to write file: ", error);
      }
    });
  } else {
    this.pdfObj.download();
  }
}

Note: The observableData variable contains values from Firebase documents.

Answer №1

If you want to retrieve data from an observable, you must first subscribe to it:

this.observableData.subscribe(data => this.realData = data);

The variable realData will store the data you need (you may need to extract it from the firebase document in a specific way).

Remember that subscribe does not instantly return data. Refer to the documentation on Observables and Subscriptions for more information:

https://rxjs.dev/guide/observable

https://rxjs.dev/guide/subscription

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

Ionic 2 - Dynamically Loading Segments

I am dealing with categories and dishes. Each dish is associated with a specific category. My goal is to send an http request to retrieve all the dishes belonging to a particular category. This will result in an array like: { Soup[{'Name',&ap ...

Guide to dynamically including a tooltip in an input box using Angular 2

I need to implement a feature where a Tooltip message is displayed when hovering over an input box. The content of the Tooltip message will depend on the response received from a service. If the service responds with 'true', the Tooltip message s ...

Using async-await in canActivate() within Angular 7

I am currently working on a code that verifies whether the browser contains user information. If not, the browser will make an API call to retrieve the user information from the server. Only users with valid information are granted access to navigate to di ...

The layout of the keyboard in Cordova has been altered

Just starting out with my first Cordova app and I'm working on a simple login form. The issue I'm facing is that whenever I click on an input element, the keyboard pops up and completely messes up my layout, which should be straightforward. Has ...

Issue with Angular 8: Unable to access property 'database' of undefined when using @firebase

Recently, I decided to upgrade my configuration from angularfire2 v4 to @angular/fire v5.2.1 and firebase from v4 to v6.2.4. Unfortunately, during this process, I encountered an issue that caused the console to log the following error message: TypeError: ...

Ways to specify certain columns for presentation in Angular Material Table

When retrieving data from a WebApi with 10 columns, I am utilizing Angular Material Grid on the Front-End. user: User; dataSource: UsersDataSource; displayedColumns = ['UserName', 'RoleName', 'ISShared', 'IsDeleted'] ...

Is it possible to integrate the Firestore npm library into my Express application?

Recently, I created my own library to act as a nosql database on my node.js web server in place of mongodb. I came across this interesting quote: Applications that use Google's Server SDKs should not be used in end-user environments, such as on pho ...

Declare that a method alters a value with state

Here's a more streamlined version of my code. Check out my custom hook below: export const useStep = () => { const [step, setStep] = useState<Steps>("sending"); const changeStep = (newStep: Steps) => setStep(newStep); return { ste ...

Access SCSS variable values in Angular HTML or TypeScript files

So, I've been looking into whether it's feasible to utilize the SCSS variable value within HTML or TS in Angular. For instance: Let's say I have a variable called $mdBreakpoint: 992px; stored inside the _variable.scss file. In my HTML cod ...

Can we trust the accuracy of the official type definition for JSON.stringify?

Upon reviewing the official type definition for JSON.stringify, it appears that it states JSON.stringify always returns a string, even when passed undefined. interface JSON { stringify(value: any, /*...*/): undefined; } However, executing JSON.stringif ...

Display items according to the visible element

Just starting with Angular and could use some guidance In my header component, I have a bootstrap navbar consisting of ul and li elements along with a router outlet. <app-header></app-header> <router-outlet></router-outlet> I&apo ...

What is the best way to navigate to another Angular page when the window width is less than 800 pixels?

I have developed an Angular 8 application that showcases a table with numerous columns. When the window size is reduced, I want to switch to a layout displaying fewer columns that can be expanded vertically to show additional information. I have already ...

Can you explain how to invoke a class with express().use function?

Currently, I am delving into learning Node JS with TypeScript but have hit a roadblock with a particular issue. In my app.ts file, I have initialized the express and attempted to call the router class inside the app.use() method, only to encounter an error ...

Is Angular capable of determining which module to load for the frontend, or does it always need to load the entire application with all modules?

Is it possible for Angular 2 to selectively load specific modules for the frontend, or does it always have to load the entire application with all modules included? ...

Allowing HTML attributes in reusable components with Vue TSX: A guide on informing Typescript

Imagine I have a custom input component: import { defineComponent } from "@vue/runtime-core" export default defineComponent({ inheritAttrs: false, setup(props, { attrs }) { return () => ( <div> ...

The eslint rule 'import/extensions' was not found in the definition

I'm encountering two errors across all TypeScript files in ESLint on VS Code: Not able to find definition for rule 'import/extensions'.eslint(import/extensions) Not able to find definition for rule 'import/no-extraneous-dependencies&apo ...

How come the statement is not displaying in the log despite the changes in the user interface?

I am currently working on a React component that looks like this: export default function Home() { const [isClicked, setIsClicked] = useState(false); console.log("Hello, World!"); const handleClick = () => { setIsClicked(true); ...

Angular 2 radio button problem with model-driven form

I'm facing a challenge with getting radio buttons to function correctly in my reactive form. Below is the code for my component: export class SettingsFormComponent implements OnInit { public sForm: FormGroup; public submitted: boolean; d ...

Ways to check my component using ActivatedRoute?

I am currently facing an issue while testing a component that utilizes two resolvers (pagesResolver and UploadResolver): Below is the code snippet for my component: export class AdminPagesComponent implements OnInit { fileUploads$: Observable<FileUpl ...

The error TS2339 occurs because the property 'remove' is not found in the type 'Document<unknown>'

I encountered an error while using my application Runtime Error: TSError: ⨯ Unable to compile TypeScript: src/controllers/notes.ts:134:20 - error TS2339: Property 'remove' does not exist on type 'Document<unknown, {}, { createdAt: Nat ...