Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Jul 27, 2018 6:21 pm

Twilio cannot read PDFs combined with Spire.PDF. They claim they are malformed PDFs:

Quote from Twilio Support:

Our engineering has done the investigation with the file and find this file is significantly malformed as a PDF.

If you open it in Preview in OSX, you'll be able to see what's wrong. Notice the first page contains the text "This is a test" "This is going to be a doc converted to pdf". Scroll down to the second page, you'll notice that the text now appears there. Scroll back up to the first page, and the text has disappeared.

This is an unrecoverable error for Programmable Fax. If we attempted to "fix" the structure, there's no guarantee around the integrity of the produced fax.

There is a blog entry describing what's structurally wrong with the PDF is https://blog.idrsolutions.com/2011/05/u ... explained/

The plugin being used to render the file in the browser is more permissive around poorly structured files


Attached are two sample files that they fail to read:
PDFs Rejected by Twilio.zip


They were produced with code like: `PdfDocument.MergeFiles(streams)` where source streams were two copies of the same simple docx file that was earlier converted to PDF by Spire too.

Are there any specific workarounds we can do to have merged files that are not "Significantly Malformed"?

We were on Spire.Office 3.4.0, but we've also tried it now with 3.7.0.

dev@filevine.com
 
Posts: 9
Joined: Wed Oct 12, 2016 9:49 pm

Mon Jul 30, 2018 6:24 am

Hi,

Thanks for your inquiry.
Note our Spire.PDF follows Adobe standard. When using Adobe Reader to open the two files you provided, there is no the phenomenon "Scroll down to the second page, you'll notice that the text now appears there. Scroll back up to the first page, and the text has disappeared." And from the description, the reason why Twilio is unable to read the the PDF generated by Spire seems to be that there is no Xref table in the result PDF. Please note, our Spire.PDF stores the Xref via stream which could generate pdf with smaller size than the pdf which has Xref table, and Xref stream is supported and reasonable way in the standard. If I misunderstand, please share more detailed information.

Besides, I noticed when closing the two PDFs without changing in Adobe Reader, it hinted "Do you want to save the changes to the file before closing". In general, there should not be the warning without changing. After testing, I didn't find the same issue when converting a Word and merging PDF. Could you please provide your input Word document and full code for generating the pdf for further investigation ?

Sincerely,
Betsy
E-iceblue support team
Last edited by Betsy.jiang on Fri Aug 03, 2018 2:12 am, edited 1 time in total.
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Jul 30, 2018 3:27 pm

We've tried merging a few different ways, all with the same result.

Latest, without even MergeFiles, simply:

Code: Select all
var combinedPdf = new PdfDocument();
            foreach (var docID in docIDs)
            {
                using (var nextStream =  retrieveAsStream(docID))
                {
                    var nextPdf = new PdfDocument(nextStream);
                    combinedPdf.AppendPage(nextPdf);
                }
            }

            using (var combinedStream = new MemoryStream())
            {
                combinedPdf.SaveToStream(combinedStream, FileFormat.PDF);
                return saveFromStream(combinedStream, title + ".pdf");
            }


Converted earlier to PDF by...

Code: Select all
                   using (var document = new Document(docStream, FileFormat.Auto))
                   {
                       using (var newPdfStream = new MemoryStream())
                       {
                            document.SaveToStream(newPdfStream, FileFormat.PDF);


Source docx's attached:
test files.zip

dev@filevine.com
 
Posts: 9
Joined: Wed Oct 12, 2016 9:49 pm

Tue Jul 31, 2018 3:54 am

Hi,

Thanks for your information.
Since your code is not complete, I changed it to below code.
Code: Select all
            //FileStream docStream = File.OpenRead("14519-test1.docx");
            FileStream docStream = File.OpenRead("14519-test2.docx");
            using (var document = new Document(docStream, Spire.Doc.FileFormat.Auto))
            {
                using (var newPdfStream = new MemoryStream())
                {
                    document.SaveToStream(newPdfStream, Spire.Doc.FileFormat.PDF);
                    var combinedPdf = new PdfDocument(newPdfStream);
                    combinedPdf.AppendPage(new PdfDocument(newPdfStream));
                    using (var combinedStream = new MemoryStream())
                    {
                        combinedPdf.SaveToStream(combinedStream, Spire.Pdf.FileFormat.PDF);
                        File.WriteAllBytes("combined-test2.pdf", combinedStream.ToArray());
                    }
                }
            }

Then I tested your case with the latest Spire.Office Platinum (DLL Only) Version:3.7.0, there was no any hint when closing the result file in Adobe Reader. Attachments were the two result PDFs. Please check if you were using the latest version. If the issue still happens, please share us a sample project for showing how you generate the PDF, and the OS and Region information, e.g. Win7 64bit, China/Chinese.

Many thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Jul 31, 2018 4:55 pm

When attempting to Fax the PDFs you have attached, through Twilio, we also get errors. No other application seems to have a problem. We will follow up with them to get a more detailed explanation as to why they say it is a "Significantly Malformed PDF".

Thank you.

dev@filevine.com
 
Posts: 9
Joined: Wed Oct 12, 2016 9:49 pm

Wed Aug 01, 2018 2:13 am

Hi,

I am waiting for your reply.
Have a nice day.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Aug 01, 2018 1:43 pm

Thank you for your patience and support.

Is there a compatibility property or setting that would force Spire to save an Xref table instead of Xref stream to see if that helps?

We've tried different CompressionLevels and also PdfConformanceLevels to no avail.

Thanks

dev@filevine.com
 
Posts: 9
Joined: Wed Oct 12, 2016 9:49 pm

Thu Aug 02, 2018 7:39 am

Hi,

Thanks for your information.
There is merely a property "CrossReferenceType" which could set the Xref table at present, and it works when creating a new PDF file. You could create a PDF file and set the property, then use the method DrawTemplate to draw the content of original file on new document. Here is my code and attachment is the result file.
Code: Select all
            List<Stream> PdfStreams = new List<Stream>();
            //convert Word to PDF stream
            string[] DocFiles = new string[]{"14519-test1.docx","14519-test2.docx"};
            for(int i = 0; i <DocFiles.Length; i ++)
            {
                string DocFile = DocFiles[i];
                FileStream DocStream = File.OpenRead(DocFile);
                MemoryStream stream = new MemoryStream();
                Document doc = new Document(DocStream,Spire.Doc.FileFormat.Docx);           
                doc.SaveToStream(stream, Spire.Doc.FileFormat.PDF);
                PdfStreams.Add(stream);
            }

            //create a new file
            PdfDocument combinedPdf = new PdfDocument();
            //set the Xref table
            combinedPdf.FileInfo.CrossReferenceType = PdfCrossReferenceType.CrossReferenceTable;           
            for (int i = 0; i < PdfStreams.Count; i++)
            {
                PdfDocument OriPdf = new PdfDocument(PdfStreams[i]);
                for (int j = 0; j < OriPdf.Pages.Count; j++)
                {
                    PdfPageBase pageOri = OriPdf.Pages[j];
                    //add a new page for combined pdf according to original page size
                    PdfPageBase CombinedPage =  combinedPdf.Pages.Add(pageOri.Size, new Spire.Pdf.Graphics.PdfMargins(0));
                    //draw the content
                    CombinedPage.Canvas.DrawTemplate(pageOri.CreateTemplate(), new System.Drawing.PointF(0,0));
                }
            }
            combinedPdf.SaveToFile("combinedPdfWithXrefTable.pdf");

Hope this helps.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Aug 02, 2018 7:51 pm

Thank you. It was worth a try, but their system rejected these files too (and others that we created with code similar to above). I hope to hear from them soon.

dev@filevine.com
 
Posts: 9
Joined: Wed Oct 12, 2016 9:49 pm

Wed Aug 15, 2018 9:39 am

Hi,

Greetings from E-iceblue.
How is the issue going now ? Could you please give us some feedback at your convenience ?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Aug 28, 2018 10:12 pm

We switched to a different PDF library that did not have any compatibility issues at all.

We may still use Spire.Office for other parts of our project still, but may end up switching it all over.

dev@filevine.com
 
Posts: 9
Joined: Wed Oct 12, 2016 9:49 pm

Wed Aug 29, 2018 1:35 am

Dear dev@filevine.com,

Thanks for your feedback.
We will look into the compatibility issue and will let you know if there is any update. Could you please provide the PDF generated by other PDF library which doesn't have the issue ?
Besides, if there is more information from Twilio, please share with us. That could help us do the investigation. Thanks in advance.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Sep 17, 2018 8:13 am

Hi,

Greetings from E-iceblue.
When investigating the issue, we encounter some troubles, we need your help, could you please provide following information ?
1) The standard Twilio used, better to provide the file which includes the detailed information of the standard.
2) All PDFs have the issue, or just specific PDF has the issue ?
3) The PDF generated by other PDF library which doesn't have the issue.
Thanks in advance.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF