galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.15k stars 170 forks source link

OnCatalogWrite event not firing #116

Open Elliot128 opened 8 years ago

Elliot128 commented 8 years ago

I want to add an acroform dictionary and some forms to my pdf. To do this I am using the OnCatalogWrite event as shown here: https://github.com/galkahana/HummusJS/wiki/Extensibility#oncatalogwrite

However, this event does not fire on pdfWriter.end() in my code. How can I get this event to fire properly so I can build the acroform?

const hummus = require('hummus')
var pdfWriter = hummus.createWriterToModify(__dirname + '/sample.pdf', {
    modifiedFilePath: __dirname + '/sampleOutput.pdf'
})
const pageModifier = new hummus.PDFPageModifier(pdfWriter, 1)

//this event fires properly
pdfWriter.getEvents().on('OnResourcesWrite', (params) => {
    console.log('On Resources Write')
})

//this event should fire on pdfWriter.end(), but does not...
pdfWriter.getEvents().on('OnCatalogWrite', (params) => {
    //can write the AcroForm dictionary to the DocumentCatalog
    console.log('got here') //doesn't print
    console.log(params) //doesn't print
})

pageModifier.startContext().getContext().writeText(
    'TESTING PDF MODIFICATIONS',
    0, 0,
    {
        size: 20, 
        colorspace: 'gray',
        color:0x00,
        font: pdfWriter.getFontForFile('/Library/Fonts/Arial.ttf')
    }
)
pageModifier.endContext().writePage()
pdfWriter.end()
console.log('done')  //prints fine
galkahana commented 8 years ago

i'll help with that. in modifications scenarios it is not guaranteed that the catalog will be rewritten (there's already one from the original PDF). for example, if no pages are added, there's no need to write a page tree and therefore the catalog.

this is why you are not getting the oncatalogwrite event.

thing is that in modifications scenarios you don't really need it. want to add an acroform to the catalog object? change it directly. create a new object with the same ID, copy all existing keys, and add acroform. so you don't have to look for an example on how to recreate an object, here's one - https://github.com/galkahana/HummusJS/blob/master/tests/ModifyingExistingFileContent.js#L100

[thirdpage object is being modified. do the same with the catalog object].

Gal.

Elliot128 commented 8 years ago

httpatomoreillycomsourceoreillyimages1821110 png Thank you for the help! :) I have this diagram from a book on PDF's. If I add text to a page, wouldn't that modify the page content, which is part of the Document Catalog?

https://github.com/galkahana/PDF-Writer/issues/62#issuecomment-240521272

Also, here is a person asking about the Acroform. You say that to create the Acfroform he must '...add a reference to the acroform from the catalog object. you do that by implementing a handler for OnCatalogWrite method of IDocumentContextExtender and attaching a listener prior to the pdfwriter ending.'

But here you say that I do not need to listen to the OnCatalogWrite event. I do not understand why he needs to use the listener to build the Acroform but I do not.

marcelotavares commented 6 years ago

PdfWriter support a way to force catalog rewrite event there is no modifications. See #239