pdfkit/docs/attachments.html
2025-04-16 16:22:41 +02:00

53 lines
5.7 KiB
HTML

<!DOCTYPE html><html><head><meta charset="utf-8"><title>Attachments in PDFKit</title><link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Source+Code+Pro:400,700|Alegreya:700|Merriweather"><link rel="stylesheet" href="/docs/css/index.css"><link rel="stylesheet" href="/docs/css/github.css"></head><body><nav class="sidebar"><ul><li><a href="/">Home</a></li><li><a href="/docs/../index.html">Documentation</a><ul><li><a href="/docs/getting_started.html">Getting Started </a></li><li><a href="/docs/paper_sizes.html">Paper Sizes</a></li><li><a href="/docs/vector.html">Vector Graphics </a></li><li><a href="/docs/text.html">Text </a></li><li><a href="/docs/images.html">Images </a></li><li><a href="/docs/outline.html">Outlines </a></li><li><a href="/docs/annotations.html">Annotations </a></li><li><a href="/docs/forms.html">Forms </a></li><li><a href="/docs/destinations.html">Destinations</a></li><li><a class="selected" href="/docs/attachments.html">Attachments </a><ul><li><a href="#embedded_files">Embedded Files</a></li><li><a href="#file_annotations">File Annotations</a></li></ul></li><li><a href="/docs/accessibility.html">Accessibility</a></li><li><a href="/docs/table.html">Tables </a></li><li><a href="/docs/you_made_it.html">You made it!</a></li></ul></li><li><a href="/docs/guide.pdf">PDF Guide</a></li><li><a href="/examples/kitchen-sink.pdf">Example PDF</a></li><li><a href="/examples/browserify/browser.html">Interactive Browser Demo</a></li><li><a href="http://github.com/foliojs/pdfkit">Source Code</a></li></ul></nav><div class="main"><h1 id="attachments_in_pdfkit">Attachments in PDFKit</h1>
<h2 id="embedded_files">Embedded Files</h2>
<p>Embedded files make it possible to embed any external file into a PDF.
Adding an embedded file is as simple as calling the <code>file</code> method and specifying a filepath.</p>
<pre><code>doc.file(path.join(__dirname, &#39;example.txt&#39;))</code></pre>
<p>It is also possible to embed data directly as a Buffer, ArrayBuffer or base64 encoded string.
If you are embedding data, it is recommended you also specify a filename like this:</p>
<pre><code>doc.file(Buffer.from(&#39;this will be a text file&#39;), { name: &#39;example.txt&#39; })</code></pre>
<p>When embedding a data URL, the <code>type</code> option will be set to the data URL&#39;s MIME type automatically:</p>
<pre><code>doc.file(&#39;data:text/plain;base64,YmFzZTY0IHN0cmluZw==&#39;, { name: &#39;base64.txt&#39; })</code></pre>
<p>There are a few other options for <code>doc.file</code>:</p>
<ul><li><code>name</code> - specify the embedded file&#39;s name</li><li><code>type</code> - specify the embedded file&#39;s subtype as a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types">MIME-Type</a></li><li><code>description</code> - add descriptive text for the embedded file</li><li><code>hidden</code> - if true, do not show file in the list of embedded files</li><li><code>creationDate</code> - override the date and time the file was created</li><li><code>modifiedDate</code> - override the date and time the file was last updated</li><li><code>relationship</code> - relationship between the PDF document and its attached file. Can be &#39;Alternative&#39;, &#39;Data&#39;, &#39;Source&#39;, &#39;Supplement&#39; or &#39;Unspecified&#39;.</li></ul>
<p>If you are attaching a file from your file system, creationDate and modifiedDate will be set to the source file&#39;s creationDate and modifiedDate.</p>
<p>Setting the <code>hidden</code> option prevents this file from showing up in the pdf viewer&#39;s attachment panel.
While this may not be very useful for embedded files, it is absolutely necessary for file annotations, to prevent them from showing up twice in the attachment panel.</p>
<h2 id="file_annotations">File Annotations</h2>
<p>A file annotation contains a reference to an embedded file that can be placed anywhere in the document.
File annotations show up in your reader&#39;s annotation panel as well as the attachment panel.</p>
<p>In order to add a file annotation, you should first read the chapter on annotations.
Like other annotations, you specify position and size with <code>x</code>, <code>y</code>, <code>width</code> and <code>height</code>, unlike other annotations you must also specify a file object.
The file object may contain the same options as <code>doc.file</code> in the previous section with the addition of the source file or buffered data in <code>src</code>.</p>
<p>Here is an example of adding a file annotation:</p>
<pre><code>const file = {
src: path.join(__dirname, &#39;example.txt&#39;),
name: &#39;example.txt&#39;,
description: &#39;file annotation description&#39;
}
const options = { Name: &#39;Paperclip&#39; }
doc.fileAnnotation(100, 100, 100, 100, file, options)</code></pre>
<p>The annotation&#39;s appearance may be changed by setting the <code>Name</code> option to one of the three predefined icons <code>GraphPush</code>, <code>Paperclip</code> or <code>Push</code> (default value).</p><nav><a class="previous" href="/docs/destinations.html">Previous</a><a class="next" href="/docs/accessibility.html">Next</a></nav></div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script><script src="/docs/js/scroll.js"></script><script src="/docs/js/highlight.pack.js"></script><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-48340245-1', 'pdfkit.org');
ga('send', 'pageview');</script></body></html>