Coder Social home page Coder Social logo

weasyprint-netcore's Introduction

Nuget

Introduction

WeasyPrint Wrapper for .Net on Windows to generate pdf from html. It uses WeasyPrint to generate pdf from html without any extra installtion and setup on Windows.

Balbarak.WeasyPrint simplifies the using of WeasyPrint on Windows

Getting started

Installation

From nuget packages

Nuget

PM> Install-Package Balbarak.WeasyPrint

Usage

From html text

using Balbarak.WeasyPrint
using System.IO;

using (WeasyPrintClient client = new WeasyPrintClient())
{
    var html = "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>";

    var binaryPdf = await client.GeneratePdfAsync(html);

    File.WriteAllBytes("result.pdf",binaryPdf);
}

From html file

using (WeasyPrintClient client = new WeasyPrintClient())
{
    var input = @"path\to\input.html";
    var output = @"path\to\output.pdf";

    await client.GeneratePdfAsync(input,output);
}

Watch output and errors

using (WeasyPrintClient client = new WeasyPrintClient())
{
    var input = @"path\to\input.html";
    var output = @"path\to\output.pdf";

    client.OnDataError += OnDataError;
    client.OnDataOutput += OnDataOutput;

    await client.GeneratePdfAsync(input,output);
}

private void OnDataOutput(OutputEventArgs e)
{
    Console.WriteLine(args.Data);
}

private void OnDataError(OutputEventArgs e)
{
    Console.WriteLine(e.Data);
}

Third Parties

weasyprint-netcore's People

Contributors

balbarak avatar looksharp avatar hizabr avatar

Stargazers

DY avatar Manfredi avatar  avatar Andreas Kluth avatar Justin Godesky avatar  avatar well.james avatar  avatar balazs HIDEGHETY avatar  avatar theMaimu avatar @Lonjoy avatar  avatar  avatar acidburn avatar Drew Hester avatar Fábio Parra dos Santos avatar Hung Doan avatar Glenn Fowler avatar Austin Allen avatar Marc Mohammad avatar Adam Jones avatar Pankaj Mane avatar Maciek Antek Papiewski avatar junichi takeda avatar  avatar Weixiao avatar Leandro Almeida avatar  avatar Artem Volkov avatar  avatar  avatar John Rodriguez avatar Kenneth Scott avatar  avatar Leandro Silva avatar Denis Dudak avatar  avatar  avatar Hunter avatar Richard Edwards avatar Giorgio Lasala avatar O0oo0O avatar  avatar Jan avatar Cohero avatar Stefano Delpero avatar Lawrence Patrick - Miami avatar Rafael Tavares avatar Tony Qu avatar Camilo E. Hidalgo Estevez avatar Jonathan Janssen avatar Chris Vanden Berghe avatar Christoph Wagner avatar Darshan avatar Walter Souza avatar Charles avatar Sutanto Gasali avatar Alex avatar Magnus Lindhe avatar Michele Vinciguerra avatar  avatar  avatar Gonçalo Prazeres avatar  avatar Saad Alothman avatar  avatar  avatar _osamabq avatar  avatar

Watchers

Lawrence Patrick - Miami avatar Magnus Lindhe avatar  avatar Stefano Delpero avatar  avatar  avatar

weasyprint-netcore's Issues

Running in a Linux container doesn't work

Thank you for creating this wrapper. It works well in my local dev env (Windows), but when I deploy it in a Linux container it fails on UnauthorizedAccessException:

 System.UnauthorizedAccessException: Access to the path '/app/balbarak-weasyprintv51' is denied.

Is there some magic I need to do for this to work properly in a Linux setup?

File generation does not work on deployment

Testing the web application locally generates the file as expected. When I deploy the code to the IIS server (windows server 2019) the code does not generate a file. It returns a null value from the method GeneratePdfAsync.

I can see the temp .html files it creates in the balbarak-weasyprintv51 directory before it tries to generate the file. It is not throwing any exception until I try and return a FileContentResult and the value from the method is null.

I have verified the App Pool User has the correct permissions to the folder hosting the app files. If I modify the PATH variables on the server, I can run the command via a Command Prompt and it works as expected.

I am out of ideas of what could be the issue.

I find it odd that the temp .html files are remaining but when I look at the source code, it should be deleting the temp files. That tells me something isn't working but the lack of exceptions has me puzzled.

Any ideas?

Permissions issue in IIS

I get this error when attempting to create a PDF from inside of IIS 10, Windows Server 2019

            using (WeasyPrintClient client = new WeasyPrintClient())
            { 
                // doc is html string
                var outputPdf = await client.GeneratePdfAsync(doc);  
            }

"Access to the path 'C:\Windows\system32\config\systemprofile\AppData\Roaming\balbarak-weasyprintv51' is denied."

This is fixable by granting access to the path from the app pool user account, but it seems like it shouldn't be trying to access that in the first place.

Great PDF generator by the way. Works great other than the issue above.

Missing license

This project currently falls into the No License case, meaning no one may reproduce, distribute, or create derivative works from your work.

Running in an Azure Function

Hello,

This isn't really an issue with the package itself but more of a question to see if anyone else has had this problem.

I've added this package to a .NET azure function, it works fine locally but as soon as it is deployed to the cloud I get these errors:

Encoding web name: ; code page: ''

Force kill process on cancellation: 'True'

Traceback (most recent call last):

return _run_code(code, main_globals, None,

exec(code, run_globals)

File "scripts\weasyprint.exe\__main__.py", line 4, in <module>

from .css import preprocess_stylesheet # noqa isort:skip

from . import computed_values, media_queries

from .. import text

import cairocffi as cairo

cairo = dlopen(

raise OSError(error_message) # pragma: no cover

OSError: no library called "cairo" was found

cannot load library 'gtk3\libcairo-2.dll': error 0x7f

cannot load library 'libcairo.so': error 0x7e

cannot load library 'libcairo.2.dylib': error 0x7e

cannot load library 'libcairo-2.dll': error 0x7f

Finished process 4688 with exit code 1, and elapsed time 00:00:01.7287848.

It looks like the dependencies are failing, I've added python 3.6.4 x64 and x86 as an extension but it didn't seem to solve the problem.

Any help would be appreciated, thanks.

Roadmap question

Hello!
Was just wondering if support for WEasyPrint would continue to catch up with later releases of WEasyPrint? Looks like WEasyPrint latest version , 51, is the minimum version i'll need because of its support for running generated content.

Also, is this library limited to what WEasyPrint can do in command line mode only?

Thank you for your work! Hope it continues :-)

JPG files are not supported

The reason given by OnDataError is (Pixbuf error: Unrecognized image file format) so I took a look into the included files and there was a gdk-pixbuf-query-loaders.exe in the gtk3 folder that instructed me that it would look in gtk3\lib\gdk-pixbuf-2.0\2.10.0\loaders\ for loaders. That directory structure didn't exist so I created it and pulled the loaders from https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/tree/master/gtk-nsis-pack/lib/gdk-pixbuf-2.0/2.10.0/loaders and tried the exe again, this time with better results:

# GdkPixbuf Image Loader Modules file
# Automatically generated file, do not edit
# Created by gdk-pixbuf-query-loaders.exe from gdk-pixbuf-2.38.0
#
# LoaderDir = C:\Users\lost\source\repos\PDFGenFromTemplate\PDFGenFromTemplat
e\bin\Debug\weasyprint-v48\gtk3\lib\gdk-pixbuf-2.0\2.10.0\loaders
#

*snip*

"bin\\Debug\\weasyprint-v48\\gtk3\\lib\\gdk-pixbuf-2.0\\2.10.0\\loaders\\libpixb
ufloader-jpeg.dll"
"jpeg" 5 "gdk-pixbuf" "JPEG" "LGPL"
"image/jpeg" ""
"jpeg" "jpe" "jpg" ""
"\377\330" "" 100

*snip*

However, even with the loader that should allow gdk-pixbuf to load a JPG into memory, WeasyPrint still gives me the same error.

Next I tried using the command line in the same manner as you do (I had a poke around in your code using dnSpy):

python.exe weasyprint.exe input.html output.pdf -e utf8

And to my surprise, but not really, the pdf rendered perfectly, JPGs and all. I'm on the right track!

Next step was to see if the issue was in the Path so I modified your code to print the Path when I call GeneratePdf. Nothing showed up in the output. Seems I don't know as much as I think I do. Over to you.

dos windows flash out when use in WinForm

first of all, thanks a lot for this .net wrapper, very handy to use.
I tried to test it from a winform application, it shows up a black command line window quickly and closes automatically.
is there a solution to this issue?

private async void button1_Click(object sender, EventArgs e) { using (WeasyPrintClient client = new WeasyPrintClient(trace)) { var html = "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>"; var fileContent = await client.GeneratePdfAsync(html); File.WriteAllBytes(@"D:\TT\temp\test.pdf", fileContent); } }

No Module named '_socket' Windows Server 2019

On Windows Server 2019 running on Azure.

All files exist after run in the roaming folder - Same issue exists running as admin.

I'm guessing its some kind of environment variables issue, but i dont know enough about the config of python to be able to diagnose the next step accurately

Info: Starting process:
Info: File name: 'cmd.exe'
Info: Arguments: '/c python.exe scripts/weasyprint.exe "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\6ec1e2b7-3f1b-4439-bbff-643e654c8981.html" "6ec1e2b7-3f1b-4439-bbff-643e654c8981.pdf" -e utf8'
Info: Working directory: 'C:\Users\username\AppData\Roaming\balbarak-weasyprintv51'
Info: Encoding web name: ; code page: ''
Info: Force kill process on cancellation: 'True'

Verbose: Process started with process id 7780, waiting for process exit.
Info: Traceback (most recent call last):
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\runpy.py", line 194, in run_module_as_main
Info: return run_code(code, main_globals, None,
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\runpy.py", line 87, in run_code
Info: exec(code, run_globals)
Info: File "scripts\weasyprint.exe_main
.py", line 4, in
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\site-packages\weasyprint_init
.py", line 21, in
Info: import html5lib
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\site-packages\html5lib_init.py", line 25, in
Info: from .html5parser import HTMLParser, parse, parseFragment
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\site-packages\html5lib\html5parser.py", line 6, in
Info: from . import _inputstream
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\site-packages\html5lib_inputstream.py", line 4, in
Info: from six.moves import http_client, urllib
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\site-packages\six.py", line 92, in get
Info: result = self._resolve()
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\site-packages\six.py", line 115, in _resolve
Info: return _import_module(self.mod)
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\site-packages\six.py", line 82, in _import_module
Info: import(name)
Verbose: STDOUT/STDERR stream read finished.
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\http\client.py", line 71, in
Info: import email.parser
Verbose: STDOUT/STDERR stream read finished.
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\email\parser.py", line 12, in
Info: from email.feedparser import FeedParser, BytesFeedParser
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\email\feedparser.py", line 27, in
Verbose: Exited process 7780 with exit code 1
Info: from email._policybase import compat32
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\email_policybase.py", line 9, in
Info: from email.utils import _has_surrogates
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\email\utils.py", line 29, in
Info: import socket
Info: File "C:\Users\username\AppData\Roaming\balbarak-weasyprintv51\lib\socket.py", line 49, in
Info: import _socket
Info: ModuleNotFoundError: No module named '_socket'
Info:
Finished process 7780 with exit code 1, and elapsed time 00:00:00.5060084.

Update WeasyPrint to latest version (v59.0)

Hi! I would like to ask, if it is possible to update the packaged WeasyPrint of the nuget package to the latest version (v59.0), because the current version is more than three years old and I need some CSS-features of later versions.

Access Denied

Hey! I'm getting
"Message":"An error has occurred.","ExceptionMessage":"Access to the path 'C:\\WINDOWS\\SysWOW64\\inetsrv\\weasyprint-v48' is denied.","ExceptionType":"System.UnauthorizedAccessException"
when trying to generate pdf from HTML text.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.