Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert PDFSharp back to iTextSharp #4467

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASENOTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Version Notes (Current Version: v2.43)
v2.43(Pre-Release)
-----------
1. fix JavaScript error when clicking on "In This Article" links in the side navigation of the default website template. (#4419)
2. Revert PDFSharp back to iTextSharp (#4407)

v2.42.1
-----------
Expand Down
64 changes: 36 additions & 28 deletions src/Microsoft.DocAsCode.HtmlToPdf/HtmlToPdfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ namespace Microsoft.DocAsCode.HtmlToPdf
using System.Threading.Tasks;
using System.Web;

using iTextSharp.text.pdf;

using Microsoft.DocAsCode.Common;
using Microsoft.DocAsCode.Plugins;

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

public class HtmlToPdfConverter
{
#region Fields

private const string OutLineKidsName = "Kids";
private const int TimeoutInMilliseconds = 60 * 1000;

private readonly HtmlToPdfOptions _htmlToPdfOptions;
Expand Down Expand Up @@ -63,7 +63,7 @@ public IDictionary<string, PartialPdfModel> GetPartialPdfModels(IList<string> ht
new ParallelOptions { MaxDegreeOfParallelism = _htmlToPdfOptions.MaxDegreeOfParallelism },
htmlFilePath =>
{
var numberOfPages = Convert($"{WrapQuoteToPath(htmlFilePath)} -", reader => reader.PageCount);
var numberOfPages = Convert($"{WrapQuoteToPath(htmlFilePath)} -", reader => reader.NumberOfPages);

PartialPdfModel pdfModel = new PartialPdfModel
{
Expand Down Expand Up @@ -170,23 +170,22 @@ private void ConvertToStream(string arguments, Stream stream)
}
}

private void CreateOutlines(PdfOutlineCollection outlineCollection, IList<HtmlModel> htmlModels, IDictionary<string, PartialPdfModel> pdfPages)
private void CreateOutlines(Dictionary<string, object> rootOutline, IList<HtmlModel> htmlModels, IDictionary<string, PartialPdfModel> pdfPages)
{
if (htmlModels?.Count > 0)
{
foreach (var htmlModel in htmlModels)
{
PdfOutline outline = new PdfOutline()
var outline = new Dictionary<string, object>
{
Title = htmlModel.Title,
Opened = true
{ "Title", htmlModel.Title },
{ OutLineKidsName, new List<Dictionary<string, object>>() }
};

if (!string.IsNullOrEmpty(htmlModel.ExternalLink))
{
outline.Elements.Add("/Type", new PdfString("/Action"));
outline.Elements.Add("/Subtype", new PdfString("/Link"));
outline.Elements.Add("/A", new PdfLiteral($"<</S/URI/URI({htmlModel.ExternalLink})>>"));
outline.Add("Action", "URI");
outline.Add("URI", htmlModel.ExternalLink);
}
else
{
Expand All @@ -204,36 +203,43 @@ private void CreateOutlines(PdfOutlineCollection outlineCollection, IList<HtmlMo
_currentNumberOfPages += pdfModel.NumberOfPages;
}

outline.DestinationPage = outlineCollection.Owner.Pages[pdfModel.PageNumber.Value];
outline.PageDestinationType = PdfPageDestinationType.FitH;
outline.Add("Action", "GoTo");

// please go to http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfDestination.html to find the detail.
outline.Add("Page", $"{pdfModel.PageNumber.Value} FitH");
}
}
}

outlineCollection.Add(outline);
CreateOutlines(outline.Outlines, htmlModel.Children, pdfPages);
((List<Dictionary<string, object>>)rootOutline[OutLineKidsName]).Add(outline);
CreateOutlines(outline, htmlModel.Children, pdfPages);
}
}
}

private void AddOutlines(PdfDocument pdfDocument)
private List<Dictionary<string, object>> ConvertOutlines()
{
var pdfFileNumberOfPages = GetPartialPdfModels(new List<string>(_htmlFilePaths));
_currentNumberOfPages = 1;

CreateOutlines(pdfDocument.Outlines, _htmlModels, pdfFileNumberOfPages);
var rootOutline = new Dictionary<string, object>
{
{ OutLineKidsName, new List<Dictionary<string, object>>() }
};

CreateOutlines(rootOutline, _htmlModels, pdfFileNumberOfPages);
return (List<Dictionary<string, object>>)rootOutline[OutLineKidsName];
}

private void CreateOutlines(PdfDocument pdfDocument)
private IList<Dictionary<string, object>> GetOutlines()
{
switch (_htmlToPdfOptions.OutlineOption)
{
case OutlineOption.NoOutline:
case OutlineOption.WkDefaultOutline:
break;
return null;
case OutlineOption.DefaultOutline:
AddOutlines(pdfDocument);
break;
return ConvertOutlines();
default:
throw new NotSupportedException(_htmlToPdfOptions.OutlineOption.ToString());
}
Expand Down Expand Up @@ -269,31 +275,33 @@ private void SaveCore(Stream stream)
{
if (_htmlFilePaths.Count > 0)
{
var outlines = GetOutlines();
using (var pdfStream = new MemoryStream())
{
ConvertToStream($"{string.Join(" ", _htmlFilePaths.Select(WrapQuoteToPath))} -", pdfStream);
pdfStream.Position = 0;

using (var pdfDocument = PdfReader.Open(pdfStream))
using (var pdfReader = new PdfReader(pdfStream))
{
CreateOutlines(pdfDocument);

pdfDocument.Save(stream);
using (var pdfStamper = new PdfStamper(pdfReader, stream))
{
pdfStamper.Outlines = outlines;
}
}
}
}
}

private T Convert<T>(string arguments, Func<PdfDocument, T> readerFunc)
private T Convert<T>(string arguments, Func<PdfReader, T> readerFunc)
{
using (var pdfStream = new MemoryStream())
{
ConvertToStream(arguments, pdfStream);
pdfStream.Position = 0;

using (var pdfDocument = PdfReader.Open(pdfStream))
using (var pdfReader = new PdfReader(pdfStream))
{
return readerFunc(pdfDocument);
return readerFunc(pdfReader);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Compile Remove="ManifestItemWithAssetId.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="PDFsharp" Version="1.50.5147" />
<PackageReference Include="iTextSharp" Version="5.5.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.DocAsCode.Build.Common\Microsoft.DocAsCode.Build.Common.csproj" />
Expand Down