2

I'm using JsPDF to export html content to a downloaded PDF. Consider the following example which takes some HTML content and outputs it to a downloaded PDF file using JsPdf

import React from "react";
import { render } from "react-dom";
import { renderToString } from "react-dom/server";
import Hello from "./Hello";
import jsPDF from "jspdf";

const styles = {
  fontFamily: "sans-serif",
  textAlign: "center"
};
const colstyle = {
  width: "30%"
};
const tableStyle = {
  width: "100%"
};
const Prints = () => (
  <div>
    <h3>Time & Materials Statement of Work (SOW)</h3>
    <h4>General Information</h4>
    <table id="tab_customers" class="table table-striped" style={tableStyle}>
      <colgroup>
        <col span="1" style={colstyle} />
        <col span="1" style={colstyle} />
      </colgroup>
      <thead>
        <tr class="warning">
          <th>SOW Creation Date</th>
          <th>SOW Start Date</th>
          <th>Project</th>
          <th>Last Updated</th>
          <th>SOW End Date</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Dec 13, 2017</td>
          <td>Jan 1, 2018</td>
          <td>NM Connect - NMETNMCM</td>
          <td>Dec 13, 2017</td>
          <td>Dec 31, 2018</td>
        </tr>
      </tbody>
    </table>
    <p>
      This is a Time and Materials Statement of Work between Northwestern Mutual
      Life Insurance Company and Infosys with all general terms and conditions
      as described in the current Master Agreement and its related documents
    </p>
  </div>
);

const print = () => {
  const string = renderToString(<Prints />);
  const pdf = new jsPDF("p", "mm", "a4");
  pdf.fromHTML(string);
  pdf.save("pdf");
};

const App = () => (
  <div style={styles}>
    <Hello name="CodeSandbox" />
    <h2>Start editing to see some magic happen {"\u2728"}</h2>
    <button onClick={print}>print</button>
  </div>
);

render(<App />, document.getElementById("root"));

My application has a much more complex solution, but this is to demonstrate the sort of content I am working with.

In my application, I have use cases where users are uploading attachments (which are saved into a MySQL database) and can be retrieved through an API. At some point in the journey, they can export to PDF some content. At some point, they may want to see their attachments inside the PDF.

Is it possible to embed a binary file inside the PDF document? If so, how can this be done? I've not come across any examples either on Stackoverflow or otherwise.

EDIT: When I refer to as 'embed a binary file', I mean to have the file be attached within the PDF. For example, in Word, you can include in the document an attachment to something else (a picture, Excel etc), and when the user double clicks on this attachment, it would open it. I'm looking to do something similar in PDF. Perhaps it would make sense to zip the binary file (if possible) before attaching it to the PDF.

I'm not sure what the limitations of Adobe are in this regard, but if this is possible, please do help.

EDIT2

I have seen this example of how to add images - is there a way to extend this so that it accepts any file types and not just images?

Adam
  • 2,384
  • 7
  • 29
  • 66
  • 1
    What would be the purpose and use of a "binary file" inside a PDF document? And where in your example code is anything like a "binary file"? – Pointy Aug 05 '22 at 13:28
  • In my application, I have use cases where users are uploading attachments (which are saved into a MySQL database) and can be retrieved through an API. At some point in the journey, they can export to PDF some content. I'm therefore looking to see how the binary file can be embedded into the PDF along with other HTML content. – Adam Aug 05 '22 at 13:32
  • I'm also unsure of where to start with adding a binary file to the PDF, so there's nothing like that in my code, hence the question. – Adam Aug 05 '22 at 13:34
  • Right, but say you get such a PDF file with a "binary file" in it. What would you expect to be able to do with that? Like, you're using Acrobat or something to look at the file; what would happen? – Pointy Aug 05 '22 at 13:42
  • @Pointy That's a good point. Essentially, I'm wondering if there is a way to be able to actually see the file within the PDF. For example, if the attachment is an image, the image would be shown in the PDF. – Adam Aug 05 '22 at 14:38
  • @Pointy after thinking it through a bit more (thank you for the points you raised that helped me do so), I've edited my question further. – Adam Aug 05 '22 at 15:42
  • @KJ thank you. However, I'm not sure I understand. I'm using JS to code this and therefore I'm not sure what the relation between that and Linux is. – Adam Aug 06 '22 at 10:59
  • @KJ I would like to do things on the client-side so again, I'm not sure how Linux is appropriate here. Maybe I'm misunderstanding your point. – Adam Aug 06 '22 at 17:59
  • @KJ so perhaps we're not on the same page or I'm misunderstanding something. I'm using `jspdf` which is outputting html content to a PDF file (like my example above). In my application, I'm. using an API to retrieve a file from a database, which is returned in the form of a binary. I'm wondering now how it may be possible to add this returned file to the PDF with `jspdf`. Are you saying that this isn't possible to do through `jspdf` (i.e. code) and the user would have to do this manually? – Adam Aug 06 '22 at 19:09

1 Answers1

1

That is not possible with jsPDF, but I can inject file such as QR Code or video etc (top right) whilst the pdf is in my browsers control (see the left hand byline, No servers were harmed in the making of this demo) Remember the pdf is always within clients editor/viewer (a.k.a. browser) not in server, that is just html text.

enter image description here

jsPDF can only build the PDF using its repertoire of commands such as place text or images such as an HTML canvas. It does not have the access to system resources such as rich media video files.

In theory you could possibly extend its functionality to ask for an upload of image file to be used like the QRCode (a binary image), but it is unlikely it could inject a rich media video file by attachment or embedment, only like chromium Edge a reference to Flip video or YourTub. enter image description here

Equally, at present, it is unlikely to allow any embedment external to the pdf application, hence I had to use the viewer and editor after the blob was displayed to add those objects to the user local"temp".pdf before the user saves the file again, but why bother the file has already been saved by the PDF viewer.

K J
  • 8,045
  • 3
  • 14
  • 36
  • Thank you so much for this information! Do you by chance have a codesandbox or a playground with this code that I can test out and modify if required? – Adam Aug 07 '22 at 10:44