最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Reactjs, generate pdf file, set name of file - Stack Overflow

matteradmin11PV0评论

Is it possible to set name of generate pdf file in "@react-pdf/renderer": "^2.3.0"?

When I click to download in toolbar, then it's save as probably with uid name: Now it set for example "f983dad0-eb2c-480b-b3e9-574d71ab162b.pdf". File is generated correctly. I would like to change the name of file to for example "name.pdf".

I use "react": "17.0.2".

import { PDFViewer } from '@react-pdf/renderer';

const App = () => (
  <PDFViewer 
    //src="/name.pdf" <- not working, error: Property 'src' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<PDFViewer> & Readonly<PDFViewerProps> & Readonly<...>'.
    //fileName="/name.pdf" <- not working, error: Property 'fileName' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<PDFViewer> & Readonly<PDFViewerProps> & Readonly<...>'.
    showToolbar={true} 
    style={{ height: "45vh", width: "100%" }}
  >
    <MyDocument />
  </PDFViewer>
);

Is it possible to set name of generate pdf file in "@react-pdf/renderer": "^2.3.0"?

When I click to download in toolbar, then it's save as probably with uid name: Now it set for example "f983dad0-eb2c-480b-b3e9-574d71ab162b.pdf". File is generated correctly. I would like to change the name of file to for example "name.pdf".

I use "react": "17.0.2".

import { PDFViewer } from '@react-pdf/renderer';

const App = () => (
  <PDFViewer 
    //src="/name.pdf" <- not working, error: Property 'src' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<PDFViewer> & Readonly<PDFViewerProps> & Readonly<...>'.
    //fileName="/name.pdf" <- not working, error: Property 'fileName' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<PDFViewer> & Readonly<PDFViewerProps> & Readonly<...>'.
    showToolbar={true} 
    style={{ height: "45vh", width: "100%" }}
  >
    <MyDocument />
  </PDFViewer>
);
Share Improve this question asked Aug 24, 2022 at 11:14 EloElo 3061 gold badge9 silver badges25 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

You can use the saveAs function inside the file-saver package to save the <Document /> as a blob with a custom name. Have a look at the saveFile function.

import { PDFViewer } from "@react-pdf/renderer";
import {
  pdf,
  Document,
  Page,
  Text,
  View,
  StyleSheet
} from "@react-pdf/renderer";
import { saveAs } from "file-saver";

const styles = StyleSheet.create({
  page: {
    flexDirection: "row",
    backgroundColor: "#E4E4E4"
  },
  section: {
    margin: 10,
    padding: 10,
    flexGrow: 1
  }
});

const MyDocument = () => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>Section #1</Text>
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
  </Document>
);

export default function App() {
  const saveFile = () => {
    // This does the trick!
    pdf(<MyDocument />)
      .toBlob()
      .then((blob) => saveAs(blob, "YourFileName.pdf"));
  };

  return (
    <div>
      <PDFViewer>
        <MyDocument />
      </PDFViewer>

      <div>
        <button onClick={saveFile}>Save File</button>
      </div>
    </div>
  );
}

You can set the title to the document:

  <Document title="Random" style={styles.document}>
    <Page size="A4" style={styles.page}>
      <View style={styles.header}>
      </View>
    </Page>
  </Document>

And now you wont have random uuid

Post a comment

comment list (0)

  1. No comments so far