Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: The root element must resolve to a <document>, actually resolved to undefined #58

Open
KirankumarDafda opened this issue Sep 3, 2021 · 1 comment

Comments

@KirankumarDafda
Copy link

KirankumarDafda commented Sep 3, 2021

I tried to generate pdf in my react js web app with the simple code

  const stream = PDFMake.createPdfKitDocument(
        JsxPdf.renderPdf(
            <document defaultStyle={{ font: 'OpenSans', fontSize: 12 }}>
                <content>This will appear in my PDF!</content>
            </document>,
        ),
      );
      
      // write the stream to a file; this could also be streamed to an HTTP connection, stdout etc
      stream.on('finish', () => console.log('PDF generated'));
      stream.pipe(fs.createWriteStream('test.pdf'));
      stream.end();

But I am getting error of Root element must be a < document >, so is there anything I am missing in the code? Please suggest a solution.

@rob-mccann
Copy link
Collaborator

rob-mccann commented Feb 27, 2022

I was able to get a PDF to render with the example (added some additional logging and changed "finish" to "end"):

import PDFMake from 'pdfmake';
import JsxPdf from 'jsx-pdf';
import { OpenSans } from './font-descriptors';
import fs from 'fs'

console.log("Running...");

const pdfMake = new PDFMake({
  OpenSans,
});

const stream = pdfMake.createPdfKitDocument(
  JsxPdf.renderPdf(
    <document defaultStyle={{ font: 'OpenSans', fontSize: 12 }}>
      <content>This will appear in my PDF!</content>
    </document>,
  ),
);

// write the stream to a file; this could also be streamed to an HTTP connection, stdout etc
stream.on('end', () => console.log('PDF generated'));
stream.on('error', (e) => console.log(e, 'asdf'));
const p = new Promise(fulfill => stream.on("end", () => fulfill()));
stream.pipe(fs.createWriteStream('./test.pdf'));
stream.end();

(async () => {
  console.log("Pending");
  await p;
  console.log("Done")
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants