3

I'm trying to display a local pdf file using a custom LocalSchemeHandler which reads a memory stream from the file.

The file exists and the memory stream is being read properly. But there is nothing being displayed in the browser window. Displaying the same file via file scheme works.

ResourceHandler:

public class LocalSchemeHandler : ResourceHandler
{
    public override bool ProcessRequestAsync(IRequest request, ICallback callback)
    {
        var uri = new Uri(request.Url);
        var file = uri.AbsolutePath;

        Task.Run(() =>
        {
            using (callback)
            {
                if (!File.Exists(file))
                {
                    callback.Cancel();
                    return;
                }

                byte[] bytes = File.ReadAllBytes(file);

                var stream = new MemoryStream(bytes);
                if (stream == null)
                {
                    callback.Cancel();
                }
                else
                {
                    stream.Position = 0;
                    ResponseLength = stream.Length;

                    var fileExtension = Path.GetExtension(file);
                    MimeType = GetMimeType(fileExtension);
                    StatusCode = (int)HttpStatusCode.OK;
                    Stream = stream;

                    callback.Continue();
                }
            }
        });

        return true;
    }
}

ISchemeHandlerFactory:

public class CustomProtocolSchemeHandlerFactory : ISchemeHandlerFactory
{
    public const string SchemeName = "local";

    public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
    {
        return new LocalSchemeHandler();
    }
}

Settings:

var settings = new CefSettings();
settings.RegisterScheme(new CefCustomScheme
{
    SchemeName = CustomProtocolSchemeHandlerFactory.SchemeName,
    SchemeHandlerFactory = new CustomProtocolSchemeHandlerFactory()
});
// Increase the log severity so CEF outputs detailed information, useful for debugging
settings.LogSeverity = LogSeverity.Default;
Cef.Initialize(settings);

EDIT Trying to display the PDF file via ResourceHandler.FromFilePath also doesn't work (nothing is displayed).

public class CustomProtocolSchemeHandlerFactory : ISchemeHandlerFactory
{
    public const string SchemeName = "local";

    public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
    {
        var uri = new Uri(request.Url);
        var file = uri.AbsolutePath;

        var fileExtension = Path.GetExtension(file);
        var mimeType = ResourceHandler.GetMimeType(fileExtension);
        return ResourceHandler.FromFilePath(file, mimeType);
    }
}

EDIT2

After setting LogSeverity to Default the log says: [0524/150955.108:INFO:CONSOLE(20)] "Refused to load plugin data from 'local://c/Users/abidh/Desktop/pdf.pdf' because it violates the following Content Security Policy directive: "object-src * blob: externalfile: file: filesystem: data:".

8
  • What is MimeType set to? Commented May 24, 2018 at 8:06
  • application/pdf Commented May 24, 2018 at 9:15
  • Try the built in implementation github.com/cefsharp/CefSharp/blob/cefsharp/63/CefSharp.Example/… Commented May 24, 2018 at 11:46
  • ResourceHandler.FromFilePath doesn't work either (see edit) Commented May 24, 2018 at 12:04
  • What does the log file say? By default it's debug.log Commented May 24, 2018 at 12:39

2 Answers 2

4

Didn't find a solution using google. Thanks to amaitland, using the IsCSPBypassing property solved the problem:

var settings = new CefSettings();
settings.RegisterScheme(new CefCustomScheme
{
    SchemeName = CustomProtocolSchemeHandlerFactory.SchemeName,
    SchemeHandlerFactory = new CustomProtocolSchemeHandlerFactory(),
    IsCSPBypassing = true
});
settings.LogSeverity = LogSeverity.Error;
Cef.Initialize(settings);
Sign up to request clarification or add additional context in comments.

Comments

1

Set the PDF file path to ChromiumWebBrowser.Address as format like file:///C:/Users/xxx/yyy.pdf, the CEFSharp will render PDF just like Chrome browser.

2 Comments

.Address property is readonly and cannot be set
This worked for me. I have the Address bound to a string property and it displays the pdf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.