# How to fix the pdf “Bad 'Length' attribute in stream” issue using MacTex tools?

Scenario: I am generating pdf using FPDI from php , where I use a template pdf to fill the form (its static not Fillable) ,I place text in required positions to give the feeling of its filled by user. which is working great except size it little extra than expected and pdf are viewable without any error or issues

New Requirement: Now I need to

• merge these files into single file
• create a bookmark to each report generated in first step (create TOC)
• possibility reset the page numbers

Obviously I need an open source tool , I don't find any opensource lib/tool( php /java/commandline ), so I installed MacTex package for pdflatex and realted tool

Whenever I include the pdf generated from php I get the errors below . My question to this great community is how I can fix this issue and what are the chances of merging the pdf files generated from php and add TOC

I have borrowed the tex file from SE by @captaincomic

Tex

\documentclass[8pt]{article}
\usepackage[final]{pdfpages}
\usepackage{fancyhdr}

\topmargin 70pt
\oddsidemargin 150pt

\pagestyle{fancy}
\rfoot{\Large\thepage}
\cfoot{}
\renewcommand {\footrulewidth}{0pt}

\begin{document}
\includepdfset{pagecommand=\thispagestyle{fancy}}
\includepdf[pages=-]{include_file.pdf}
\end{document}


> pdflatex number.tex

Output Error

  ....
<include_file.pdf, id=3, page=1, 614.295pt x 794.97pt>
<use include_file.pdf, page 1> <use include_file.pdf, page 1>
<use include_file.pdf, page 1> <use include_file.pdf, page 1>
<use include_file.pdf, page 1> [1{/usr/local/texlive/2011/texmf-var/fonts/map/p
dftex/updmap/pdftex.map} <./include_file.pdfError (2874915): Bad 'Length' attribute in stream

!pdfTeX error: pdflatex (file ./include_file.pdf): PDF inclusion: type <error>
cannot be copied
==> Fatal error occurred, no output PDF file produced!

-
The error message doesn't come from TeX, but from the library (probably xpdf) which does the pdf inclusion. I assume the pdf to be included is really broken. Have you tried to open/process it with another tool? Maybe you could make the file available for inspection? – Stephan Lehmke May 19 '12 at 8:26
@StephanLehmke thanks to drop a comment. Would you suggest how I can inspect the pdf file ? The file is viewable perfectly in Acrobat ,and other pdf viewers. thanks – sakhunzai May 21 '12 at 6:55
That the file displays without error in acrobat isn't a good sign because it might mean there is a bug in the pdf library used by pdftex :-( What I meant by making the file available was to upload it somewhere so people could look at it for the reason of the inclusion error. – Stephan Lehmke May 21 '12 at 9:00
I ll ask my client . However,using tip: thomer.com/howtos/embed_font_in_pdf.html. I reduced the size of pdf files.After merging the pdf using FPDF_TPL: setasign.de/products/pdf-php-solutions/fpdi/downloads . when I try to run any of pdf* command e.g pdf2ps I get the following error : Error: /typecheck in --run-- Operand stack: --dict:5/14(L)-- F2 14.12 --dict:5/5(L)-- --dict:5/5(L)-- DejaVuSans-Bold-UCS . This seems to be embeded font issue. As I am using DejaVu fonts . – sakhunzai May 21 '12 at 12:08
It seems I have to subscribe to something to access the file... – Stephan Lehmke May 22 '12 at 11:57

I successfully repaired include_file.pdf using the command line tool pdftk:

pdftk include_file.pdf output include_file_fixed.pdf


or command line tool qpdf:

qpdf include_file.pdf include_file_fixed.pdf


Both tools recompress all stream objects within a PDF file, thereby renewing the value of the /Length entry in the associated stream dictionary of the stream object. qpdf reports wrong \Length values in 12 stream objects.

With the fixed file the error disappears.

-
thanks to point out that , but I don't think I ll able to use that due to licensing restrictions. I need an "Free Open Source" solution that does not have licensing restrictions. To my knowledge pdftk is based on iText and iText is fairly limited for commercial applications. Please correct me if I am wrong. Thanks again – sakhunzai May 24 '12 at 8:09
@sakhunzai: pdftk is GPL and iText is AGPL. Where is the problem? – AlexG May 24 '12 at 8:57
@sakhunzai: qpdf is Artistic License. Does this fit? – AlexG May 24 '12 at 9:10
I am not expert in this domain but the iText licence page says Buying such a license is mandatory as soon as you develop commercial activities distributing the iText software inside your product or deploying it on a network without disclosing the source code of your own applications under the AGPL license. These activities include: offering paid services to customers as an ASP, serving PDFs on the fly in the cloud or in a web application, shipping iText with a closed source product. – sakhunzai May 24 '12 at 10:04
qpdf licence seems to be reasonable Distributor Fees are permitted, and licensing fees for other components in the aggregation are permitted. The terms of this license apply to the use and Distribution of the Standard or Modified Versions as included in the aggregation. Do you think qpdf can solve this issue ? – sakhunzai May 24 '12 at 10:10