scryber.core icon indicating copy to clipboard operation
scryber.core copied to clipboard

Allow fonts from a Base64 string

Open 0NotApplicable0 opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe. You are unable to load a font from a Base64 string.

Ex:

@font-face {
            font-family: 'code128';
            src: url(data:font/truetype;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMl8G62MAAAE4AA...)
}

Describe the solution you'd like Create a new FontSourceType "Base64" this can be found during TryParseOneValue() using logic such as if (value.Contains("base64") && value.Contains("data:")) typeS = "base64";

Then during FullfillRequest() check the ResourceType of the request

if (request.ResourceType == "Base64") {
this.FullfillDataRequest(request);
}

Then create a new function as such:

        // TODO: documentation
        protected virtual bool FullfillDataRequest(RemoteFileRequest dataRequest)
        {
            if(this.LogDebug)
                this.AddDebugLog("SYNC fulfilling the request for the DATA '" + dataRequest.FilePath + "' as this is not an async execution");

            var bytes = Convert.FromBase64String(dataRequest.FilePath.Split(',')[1]);
            using (var stream = new MemoryStream(bytes))
            {
                if(this.LogDebug)
                    this.AddDebugLog( "Stream received from data '" + dataRequest.FilePath + "' and starting the callback");

                var success = dataRequest.Callback(this._owner, dataRequest, stream);
                
                if(this.LogDebug)
                    this.AddDebugLog( "Callback done for data '" + dataRequest.FilePath + "' and reported " + (success ? "SUCCESS" : "FAIL"));

                dataRequest.CompleteRequest(dataRequest.Result, success);

                if(this.LogDebug)
                    this.AddDebugLog( "Completed the request for data '" + dataRequest.FilePath + "' with result" + (dataRequest.Result == null ? "NO RESULT SET" : dataRequest.Result.ToString()));

                return success;
            }
        }

It is basically a copy of FullfillFileRequest but uses MemoryStream instead of File.OpenRead.

Describe alternatives you've considered None

Additional context I have implemented what I described above and will submit a pull request after some more testing of the solution. It was a quick and dirty implementation and will check for better solutions.

0NotApplicable0 avatar Apr 27 '22 23:04 0NotApplicable0

Also @richard-scryber I have tried many different open source ways to convert HTML to PDFs and this is by far the best way to do it, mostly.

Using Puppeteer or WKHtmlToPDF probably gives more freedom to what CSS/HTML can be used but they are significantly slower 4-5 seconds for 40 pages to 200ms. My application of this needs to be fast and I came across scryber.core which is exactly what I need.

TL;DR: As I continue to use scryber.core I might come across things that could be added, mostly more support for CSS3/HTML5 stuff. I would love to help more, if you have anything particular you want added give me a ping. Maybe I could get some contact information so I can help out more.

This library is great!

0NotApplicable0 avatar Apr 27 '22 23:04 0NotApplicable0

@0NotApplicable0 - I have now merged your pull into the master branch. Thank you for doing this, and your kind comments also! I'll reach out too.

richard-scryber avatar Jan 17 '23 12:01 richard-scryber