Adobe Reader cannot open .pdf file created using mPDF in Zend Framework - zend-framework

I'm trying to generate a .pdf file using mPDF in a Zend Framework application, from the output of the action.
Here is the code of my action:
public function testAction()
{
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$this->view->foo = 'bar';
$this->render();
$output = $this->getResponse()->getBody();
$layout = new Zend_Layout();
$layout->content = $output;
$layout->setLayoutPath(dirname(dirname(__FILE__)) . '/views/layouts/');
$layout->setViewSuffix('tpl');
$layout->setLayout('pdf');
$html = $layout->render();
$mpdf = new mPDF('utf-8', 'A4');
$mpdf->WriteHTML($html);
$mpdf->Output('report.pdf', 'D');
}
If the content to be displayed is long (i.e. a few paragraphs), when downloading the .pdf file, Adobe Reader throws the following error: Adobe Reader could not open 'report.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
However, if I pass the same output to mpdf as a static variable, without using Zend Layout render, then the .pdf file opens without any errors. Also, Adobe Reader throws the issue if and only if the content is long (i.e. it works if there are only a couple of words).
Is there any limit I should be aware of?

there should not be space at beginning and at the end of file so check these space,

Adobe Reader is less forgiving than some othe PDF readers if the PDF file is corrupt. Open your PDF document in a text editor and check that the file starts with something like:
%PDF-1.4
%âãÃÓ
Sometimes PHP error notices are found at the top of the file.
Source : mpdf forum IanBack's answer

Related

WordprocessingDocument.CreateFromTemplate method creates corrupted MS Word files

I have proper .dotm template.
When I create a new file based on a template by double clicking in explorer it creates the correct file (based on this template). Created file size after save is 16Kb (without any content).
But if I want to use .CreateFromTemplate method in my code I cannot open a newly created .docx file in MS Word.
New file size is 207Kb (just like .dotm file). MS Word display "run-time error 5398" and not open the file.
I'm using nuget package DocumentFormat.OpenXml 2.19.0, Word 365 version 16.0.14931.20648 - 32bit and code like this:
using (WordprocessingDocument doc = WordprocessingDocument.CreateFromTemplate(templatePath))
{
doc.SaveAs(newFileName);
}
Google is silent about this error, ChatGPT says that:
The "Run-time Error 5398" error means that the file you are trying to open is corrupted or not a valid docx file. Possible reasons for this error may be the following:
The file was not saved correctly after making changes. Verify that the Save() method was called after making changes to the file.
The file was saved with the wrong extension, e.g. as DOTM instead of DOCX
The file was saved in an invalid format.
There may have been some unhandled exceptions in your code.
When I manually change the extension of a new file from docx to dotm, there is no error when opening, but the file does not open.
What am I doing wrong with CreateFromTemplate method?
I tried to reproduce the behavior you described, using the following unit tests:
public sealed class CreateFromTemplateTests
{
private readonly ITestOutputHelper _output;
public CreateFromTemplateTests(ITestOutputHelper output)
{
_output = output;
}
[Theory]
[InlineData("c:\\temp\\MacroEnabledTemplate.dotm", "c:\\temp\\MacroEnabledDocument.docm")]
[InlineData("c:\\temp\\Template.dotx", "c:\\temp\\Document.docx")]
public void CanCreateDocmFromDotm(string templatePath, string documentPath)
{
// Let's not attach the template, which is done by default. If a template is attached, the validator complains as follows:
// The element has unexpected child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:attachedTemplate'.
using (var wordDocument = WordprocessingDocument.CreateFromTemplate(templatePath, false))
{
// Validate the document as created with CreateFromTemplate.
ValidateOpenXmlPackage(wordDocument);
// Save that document to disk so we can open it with Word, for example.
wordDocument.SaveAs(documentPath).Dispose();
}
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentPath, true))
{
// Validate the document that was opened from disk, just to see what Word would open.
ValidateOpenXmlPackage(wordDocument);
}
}
private void ValidateOpenXmlPackage(OpenXmlPackage openXmlPackage)
{
OpenXmlValidator validator = new(FileFormatVersions.Office2019);
List<ValidationErrorInfo> validationErrors = validator.Validate(openXmlPackage).ToList();
foreach (ValidationErrorInfo validationError in validationErrors)
{
_output.WriteLine(validationError.Description);
}
if (validationErrors.Any())
{
// Note that Word will most often be able to open the document even if there are validation errors.
throw new Exception("The validator found validation errors.");
}
}
}
In both tests, the documents are created without an issue. Looking at the Open XML markup, both documents look fine. However, while I don't get any runtime error, Word also does not open the macro-enabled document.
I am not sure why that happens. It might be related to your security settings.
Depending on whether or not you really need to use CreateFromTemplate(), you could create a .docm (rather than a .dotm) and create new macro-enabled documents by copying that .docm.
I opened an issue in the Open XML SDK project on GitHub.

The PDFViewer is not showing the content while downloading is possible

I am trying to use a PDFViewer in a SAPUI5 application, something like the following sample app.
When I try to use this component in Google chrome it will not load the data, however it is possible to download the PDF itself and it shows the url works and file is available.
If I open it in Firefox or IE it works!
I discussed the problem here with SAP OpenUI5 team. Finally we understood the problem is not the UI5 library and the problem is inside of our ABAP implementation that provides download link for the PDF file from SAP Document Management System (SAP DMS).
We finally found the solution and discovered why the pdf that we try to show from our SAP DMS is downloadable while it is not shown in the pdf viewer inside the modern browsers like Chrome or Firefox.
The source for the solution can find here.
The below two changes are different from normal implementation that can be found in most of the tutorials in the internet:
The header value must be changed to Inline;filename= instead of outline;filename.
Call the method /IWBEP/IF_MGW_CONV_SRV_RUNTIME=>Set_header to set the header.
Finally we have the following ABAP code in SAP systems for downloading files form document management system (SAP DMS).
"Logic for Download the files from Document Managmenet System
DATA: ls_lheader TYPE ihttpnvp,
ls_stream TYPE ty_s_media_resource,
ls_entity TYPE zgw_odata_document_file.
CONSTANTS: lc_headername TYPE string VALUE 'Content-Disposition',
lc_headervalue1 TYPE string VALUE 'inline; filename="',
lc_headervalue2 TYPE string VALUE '";'.
* "Get the name of the Entity
DATA(lv_entity_name) = io_tech_request_context->get_entity_type_name( ).
CASE lv_entity_name.
WHEN 'DocumentFile'.
DATA(lo_document_file) = NEW zcl_gw_odata_document_file( ).
lo_document_file->read_stream(
EXPORTING
it_key_tab = it_key_tab
IMPORTING
es_stream = ls_entity ).
ls_lheader-name = lc_headername.
ls_entity-file_name = escape( val = ls_entity-file_name format = cl_abap_format=>e_url ).
ls_lheader-value = lc_headervalue1 && ls_entity-file_name && lc_headervalue2 .
set_header( is_header = ls_lheader ).
ls_stream-mime_type = ls_entity-mimetype.
ls_stream-value = ls_entity-binfile.
copy_data_to_ref( EXPORTING is_data = ls_stream
CHANGING cr_data = er_stream ).
WHEN OTHERS.
ENDCASE.

Aspose.Cells - Opening OpenXml file generated by EPPlus

I generated an Excel spreadsheets in OpenXml format using EPPlus. When I try to open it using Aspose.Cells, the program returns the following error:
Aspose.Cells.CellsException: ‘Invalid cell name’
I believe the cells didn’t find the correct file format.
How can I open this file?
I am using C# and .NET Core
Thanks
I am Ahsan Iqbal from Aspose.
I have tested this issue with my own sample code but this issue is not reproduced.
static void Main(string[] args)
{
Console.WriteLine("Creating XLSX file using EPPlus");
CreateExcelFileInEpplus();
Console.WriteLine("Success in creating XLSX file using EPPlus");
ReadExcelFileUsingAsposeCells();
Console.WriteLine("Success in reading XLSX file using EPPlus");
}
static void CreateExcelFileInEpplus()
{
var newFile = new FileInfo("output.xlsx");
if (File.Exists(newFile.Name))
File.Delete(newFile.Name);
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
xlPackage.Workbook.Worksheets.Add("Sheet1");
// do work here
xlPackage.Save();
}
}
static void ReadExcelFileUsingAsposeCells()
{
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook("output.xlsx");
Console.WriteLine(workbook.Worksheets[0].Name);
}
This test shows that Aspose.Cells can read the Excel spreadsheet generated by EPPlus. Your sample code and output files are required for analysis. You may please create a thread at Aspose.Cells Forum and share your code, output XLSX file, version of Aspose.Cells and .NET Core, operating system and other specific environment information if any. Our support team will assist you further in this regard.

Including an embedded image in an Outlook HTML email via Perl

I need to generate an HTML email with a banner image embedded. It must go through an Outlook2007 mail client. I tried to base64encode the image and put it inline (it looked good) but Outlook would not send the email. I have culled through many different articles (in various programming languages) that have gotten me to this point but it is still not working. This code creates the email and attaches the image but the image is not displayed.
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';
my $oMailer = new Win32::OLE('Outlook.Application') or
die "Unable to start an Outlook instance: $!\n";
my $oEmail = $oMailer->CreateItem(0) or
die "Unable to create mail item: $!\n";
$oEmail->{'To'} = 'me#here.org';
$oEmail->{'Subject'} = "Embedded image test";
$oEmail->{'BodyFormat'} = olFormatHTML;
$oEmail->{'HTMLBody'} = "<html><body><img src=\"cid:banner.jpg\"></body></html>";
my $attachments = $oEmail->Attachments();
my $bannerAttachment = $attachments->Add('C:/test/banner.jpg', olEmbeddeditem);
$bannerAttachment->PropertyAccessor->SetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x3712001E", "banner.jpg");
$oEmail->save();
(BTW, I removed all the Win32::OLE->LastError() checks before posting because none of them failed anyway.)
When adding the attachment, it does not set the attachment Type to olEmbeddeditem (5); Don't know if this is relevant to the problem.
The SetProperty does not set the value either. That is supposed to set the Content ID (cid) that is referenced in the img src in the HTML. I used the below code to GetProperty and it returns an empty string.
my $CIDvalue = $bannerAttachment->PropertyAccessor->GetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x3712001E");
print ">>>CIDvalue = $CIDvalue\n";
So close I can taste it!
Careful reading in the Perl docs for WIN32::OLE revealed a SetProperty method that was apparently being called instead of the M$ one I thought I was calling. Changing the code to:
$bannerAttachment->PropertyAccessor->Invoke('SetProperty', "http://schemas.microsoft.com/mapi/proptag/0x3712001E", "banner.jpg");
made it work and there was great rejoicing :)

Open a pdf from the Help menu in a RCP application

I included a HELP_CONTENTS action from the ActionFactory in my RCP application but I would like to open a PDF file when I call it in the application and not the normal xml file. Is that possible? I didn't find any examples online.
If you use HelpContentsAction then you'll get Eclipse help. If you want to display your own file then create your own action (you can reuse the icon and text if you want to).
Use IWorkbenchPage.openEditor(IEditorInput input, String editorId) method to open PDF file:
org.eclipse.core.resources.IFile workspaceFile;
java.io.File externalFile;
//PDF in workspace
IEditorInput input = new FileEditorInput(workspaceFile);
//or external
IFileSystem fs = org.eclipse.core.filesystem.EFS.getLocalFileSystem();
IFileStore fileStore = fs.fromLocalFile(externalFile);
input = new FileStoreEditorInput(fileStore);
IWorkbenchPage page =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.openEditor(input, IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);