Skip to content

Invalid ColorSpace error in PDF files in Acrobat or Reader #1601

Open

Description

@ggvattenfall

Bug Report

Invalid ColorSpace error in PDF files in Acrobat or Reader

Description of the problem

There is an error displayed in Adobe Reader 'Invalid ColorSpace error in PDF files in Acrobat or Reader' while opening the PDF/A-3a file generated in PDFKit. When I remove the subset of PDF/A-3a Adobe doesn't throw any errors and opens the file correctly.

I'm working in latest angular version and latest pdfkit.

Code sample


import blobStream from 'blob-stream';
import pdfkit from 'pdfkit';


 async createPdf(htmlDoct: string) {
    const doc = new pdfkit({
      pdfVersion: '1.7',
      subset: 'PDF/A-3a',
      tagged: true,
      displayTitle: true,
      size: 'A4',
      bufferPages: true,
      margin: 30,
    });
   // draw some text
   doc.fontSize(25).text('Here is some vector graphics...', 100, 80);

   // some vector graphics
   doc
     .save()
     .moveTo(100, 150)
     .lineTo(100, 250)
     .lineTo(200, 250)
     .fill('#FF3300');
 
   doc.circle(280, 200, 50).fill('#6600FF');
 
   // an SVG path
   doc
     .scale(0.6)
     .translate(470, 130)
     .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
     .fill('red', 'even-odd')
     .restore();
 
   doc.save();
   // a gradient fill
   var gradient = doc
     .linearGradient(100, 300, 200, 300)
     .stop(0, 'green')
     .stop(0.5, 'red')
     .stop(1, 'green');
   doc.rect(100, 300, 100, 100).fill(gradient);
   let stream = doc.pipe(blobStream());

   // stroke & fill uncolored tiling pattern
 
   var stripe45d = doc.pattern(
     [1, 1, 4, 4],
     3,
     3,
     '1 w 0 1 m 4 5 l s 2 0 m 5 3 l s'
   );
   doc.circle(280, 350, 50).fill([stripe45d, 'blue']);
 
   doc
     .rect(380, 300, 100, 100)
     .fillColor('lime')
     .strokeColor([stripe45d, 'orange'])
     .lineWidth(5)
     .fillAndStroke();
   doc.restore();
 
   // and some justified text wrapped into columns
   doc
     .text('And here is some wrapped text...', 100, 450)
     .font('Times-Roman', 13)
     .moveDown()
     .text('lorem', {
       width: 412,
       align: 'justify',
       indent: 30,
       columns: 2,
       height: 300,
       ellipsis: true
     });
 
    doc.end();
    stream.on('finish', () => {
      window.open(stream.toBlobURL('application/pdf'));
    });
  }

my webpack config export file :



     module.exports = {
  plugins: [
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
      process: 'process/browser',
    }),
    new webpack.DefinePlugin({
      output: {
        path: path.resolve(__dirname, '/dist/apps/' + project),
      },
    }),
  ],
  resolve: {
    symlinks: false,
    alias: {
      // maps fs to a virtual one allowing to register file content dynamically
      fs: __dirname +  '/virtual-fs.js'
    },
    fallback: {
      // crypto module is not necessary at browser
      crypto: false,
      // fallbacks for native node libraries
      buffer: require.resolve('buffer/'),
      stream: require.resolve('readable-stream'),
      zlib: require.resolve('browserify-zlib'),
      util: require.resolve('util/'),
      assert: require.resolve('assert/'),
      fs: require.resolve('fs'),
    },
  },

  module: {
    rules: [
      { test: /\.afm$/, type: 'asset/source' }, 
      { test: /\.icc$/, type: 'asset/source' },  
      { test: /\.png$/, type: 'asset/source' },
      {
        test: /src[/\\]static-assets/,
        type: 'asset/inline',
        generator: {
          dataUrl: (content) => {
            return content.toString('base64');
          },
        },
      },
      // load binary files inside lazy-assets folder as an URL
      {
        test: /src[/\\]lazy-assets/,
        type: 'asset/resource',
      },
    ],
  },
};

Your environment

  • pdfkit version: 0.16.0
  • Node version:
  • Browser version (if applicable):
  • Operating System:

Activity

  1. andreiaugustin commented on Mar 24, 2025

    @andreiaugustin
    Contributor

    I've tried the following in a NodeJS script and I don't see any errors in Adobe Acrobat Reader, is this a Web-only issue or is there a specific area in Adobe Acrobat Reader where you see the error?

    
    const PDFDocument = require('pdfkit');
    const fs = require('fs')
    
    var pdfOptions = {
        autoFirstPage: false,
        permissions: {
            printing: 'highResolution',
            modifying: false,
            copying: true,
            annotating: false,
        },
        pdfVersion: '1.7',
        subset: 'PDF/A-3a',
        tagged:true,
        displayTitle:true,
        size:'A4',
        bufferPages:true,
        margin:30
    };
    
    // Create a document
    const doc = new PDFDocument(pdfOptions);
    doc.info.Author = 'me';
    doc.info.Title = 'test';
    doc.info.Subject = 'PDFKit';
    
    doc.pipe(fs.createWriteStream('C:\\Users\\andrei\\Desktop\\testout.pdf'));
    
    // Start of page 1
    doc.addPage({size: [612.013493,792.017462], margin: 0});
    
    // draw some text
    doc.fontSize(25).text('Here is some vector graphics...', 100, 80);
    
    // some vector graphics
    doc
    .save()
    .moveTo(100, 150)
    .lineTo(100, 250)
    .lineTo(200, 250)
    .fill('#FF3300');
    
    doc.circle(280, 200, 50).fill('#6600FF');
    
    // an SVG path
    doc
    .scale(0.6)
    .translate(470, 130)
    .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
    .fill('red', 'even-odd')
    .restore();
    
    doc.save();
    // a gradient fill
    var gradient = doc
    .linearGradient(100, 300, 200, 300)
    .stop(0, 'green')
    .stop(0.5, 'red')
    .stop(1, 'green');
    doc.rect(100, 300, 100, 100).fill(gradient);
    
    // stroke & fill uncolored tiling pattern
    
    var stripe45d = doc.pattern(
    [1, 1, 4, 4],
    3,
    3,
    '1 w 0 1 m 4 5 l s 2 0 m 5 3 l s'
    );
    doc.circle(280, 350, 50).fill([stripe45d, 'blue']);
    
    doc
    .rect(380, 300, 100, 100)
    .fillColor('lime')
    .strokeColor([stripe45d, 'orange'])
    .lineWidth(5)
    .fillAndStroke();
    doc.restore();
    
    // and some justified text wrapped into columns
    doc
    .text('And here is some wrapped text...', 100, 450)
    .font('Times-Roman', 13)
    .moveDown()
    .text('lorem', {
    width: 412,
    align: 'justify',
    indent: 30,
    columns: 2,
    height: 300,
    ellipsis: true
    });
    
    
    doc.end();
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions