This is a migrated thread and some comments may be shown as answers.

Error in PDFFormatProvider when exporting HTML with incorrect URLs

2 Answers 316 Views
Visual Studio Extensions
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Iron
Marcel asked on 02 Jul 2020, 10:06 AM

Hi, I'm having the following problem. When calling

 

pdfProvider.Export(document, stream);

 

where pdfProvider is PdfFormatProvider and document is RadFlowDocument, I sometimes get an Illegal Path exception with this stack trace:

 

   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.IO.Path.GetExtension(String path)
   at Telerik.Windows.Documents.Media.UriImageSource.UpdateExtensionFromUri()
   at Telerik.Windows.Documents.Media.UriImageSource.GetImageData()
   at Telerik.Windows.Documents.Media.UriImageSource.get_Data()
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExportContext.GetImageSource(ImageSource imageSource)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.ExportImageInline(ImageInline imageInline, Block block)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.ExportInline(RadFixedDocumentEditor editor, InlineBase inline, Block block)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.ExportInlines(RadFixedDocumentEditor editor, Paragraph paragraph, Block block)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.ExportParagraph(RadFixedDocumentEditor editor, Paragraph paragraph, Paragraph nextParagraph, Boolean isFirstBlockInContainer)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.ExportBlock(RadFixedDocumentEditor editor, BlockBase block, BlockBase nextBlock, Boolean isFirstBlockInContainer)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.<ExportBlockContainer>d__5.MoveNext()
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.ExportSection(Section section, RadFixedDocumentEditor editor)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.ExportDocument(RadFlowDocument document, RadFixedDocumentEditor editor)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.Export.PdfExporter.Export()
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider.ExportOverride(RadFlowDocument document, Stream output)
   at Telerik.Windows.Documents.Common.FormatProviders.FormatProviderBase`1.Export(T document, Stream output)
   at LS.TMMsgViewer.Services.PdfGenerator.ExportToStream(Stream stream) in C:\ls\pvminiserver\LS.TMMsgViewer\Services\PdfGenerator.cs:line 63

 

The HTML contains this fragment, I suspect the URL in the "src" attribute is what's causing the problem:

 

<p class=MsoNormal>
            <img border=0 width=1 height=1 id="_x0000_i1033" src="https://t.myvisualiq.net/sync?prid=Test&amp;ao=0&amp;red=https://ad.doubleclick.net/ddm/trackimp/N7121.2426714VISUALIQ0/B20904629.217713024;dc_trk_aid=415907722;dc_trk_cid=99313089;ord=%20180907151105;u=25_CE_35_NFA180907|VIQ_$%7bUUID%7d|;dc_lat=;dc_rdid=;tag_for_child_directed_treatment=?" alt=Advertisement>
            <o:p></o:p>
         </p>

 

Is there a setting to ignore malformed URLs (instead of crashing)?

Thanks.

2 Answers, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 02 Jul 2020, 01:40 PM

Hello Marcel,

It seems (as you suggested) this behavior is caused due to an illegal path to an image. I logged an item in our backlog to fix this: WordsProcessing: ArgumentException is thrown when exporting a RadFlowDocument that contains an image with an invalid Uri. Please, make sure to follow the item using the Follow button so you can receive updates about its status changes.

Until this issue is fixed I could suggest you as a workaround to check if the path is valid and if not, to remove the image from the RadFlowDocument

foreach (ImageInline image in document.EnumerateChildrenOfType<ImageInline>().ToList())
{
	UriImageSource uriImageSource = (UriImageSource)image.Image.ImageSource;
	if (uriImageSource != null && !IsValid(uriImageSource.Uri.OriginalString))
	{
		image.Paragraph.Inlines.Remove(image);
	}
}
private static bool IsValid(string uri)
{
	try
	{
		Path.GetExtension(uri);
	}
	catch (ArgumentException)
	{
		return false;
	}

	return true;
}

I am attaching a sample project that demonstrates this functionality.

In appreciation for bringing this to our attention, I updated your Telerik points.

Regards,
Martin
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Marcel
Top achievements
Rank 1
Iron
answered on 06 Jul 2020, 05:38 AM
Thank you, this seems to be working.
Tags
Visual Studio Extensions
Asked by
Marcel
Top achievements
Rank 1
Iron
Answers by
Martin
Telerik team
Marcel
Top achievements
Rank 1
Iron
Share this question
or