You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用纯JavaScript生成PDF 1.6/1.7等新版本文件?

Generating PDF 1.6/1.7 (and Newer) with Pure JavaScript

Great question—this is a common pain point since most popular JS PDF libraries default to older specs for broader compatibility. Let’s break down your questions with practical, pure-JavaScript solutions:

1. Generating PDF 1.6 or 1.7 Files

You don’t need to rely on C++-backed tools like HummusJS. Here are two solid pure-JS options:

pdf-lib (Browser & Node.js)

This modern, pure-JS library explicitly supports setting PDF versions from 1.3 up to 2.0. It’s lightweight and flexible, with excellent documentation.

Example code to create a PDF 1.7 document:

import { PDFDocument } from 'pdf-lib';

async function generatePDF17() {
  // Initialize a new PDF document with version 1.7
  const pdfDoc = await PDFDocument.create({ pdfVersion: '1.7' });
  
  // Add a page and content
  const page = pdfDoc.addPage([612, 792]); // Standard US Letter size
  page.drawText('Generated with PDF 1.7 (pure JS!)', {
    x: 50,
    y: 750,
    size: 20,
  });
  
  // Save the document to bytes
  const pdfBytes = await pdfDoc.save();
  
  // Example: Download the PDF in the browser
  const blob = new Blob([pdfBytes], { type: 'application/pdf' });
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = 'pdf-1.7-example.pdf';
  a.click();
  URL.revokeObjectURL(url);
}

pdfkit (Node.js)

While you noted pdfkit tops out at 1.3/1.4 by default, it actually supports specifying newer versions via a configuration option. Just set the version parameter when initializing the document:

const PDFDocument = require('pdfkit');
const fs = require('fs');

// Create a PDF 1.6 document
const doc = new PDFDocument({ version: '1.6' });

// Pipe output to a file
doc.pipe(fs.createWriteStream('pdf-1.6-example.pdf'));

// Add content
doc.fontSize(20).text('Hello from PDF 1.6!', 50, 50);

// Finalize the document
doc.end();

Note: Double-check your pdfkit version—older releases might not support this, so update to the latest version if needed.

2. Generating Newer PDF Formats (e.g., PDF 2.0)

Yes, pure-JavaScript solutions for newer formats like PDF 2.0 (released in 2017) exist, though they’re less widely adopted than older spec support.

Again, pdf-lib leads here—you can simply set pdfVersion: '2.0' when creating a document. Keep in mind that PDF 2.0 support in readers is still growing (most modern tools like Adobe Acrobat, Chrome, and Firefox do support it), so test with your target audience’s tools if compatibility is critical.

Why Most Libraries Default to Older Versions

The main reason popular libraries stick to PDF 1.3/1.4 is broad compatibility. Many legacy systems, older printers, and niche devices still struggle with newer PDF specs. If you don’t need features exclusive to 1.6+/2.0, sticking with the default is safer—but if you do require those features, the options above work well.


内容的提问来源于stack exchange,提问作者Petr Gazarov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:00:57