mirror of
https://github.com/unidoc/unihtml.git
synced 2025-04-24 13:48:52 +08:00
79 lines
9.4 KiB
Go
79 lines
9.4 KiB
Go
//
|
|
// Copyright 2020 FoxyUtils ehf. All rights reserved.
|
|
//
|
|
// This is a commercial product and requires a license to operate.
|
|
// A trial license can be obtained at https://unidoc.io
|
|
//
|
|
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
|
|
//
|
|
// Use of this source code is governed by the UniDoc End User License Agreement
|
|
// terms that can be accessed at https://unidoc.io/eula/
|
|
|
|
// Package unihtml contains a plugin for the UniDoc.
|
|
package unihtml ;import (_b "bytes";_c "context";_fc "errors";_db "fmt";_g "github.com/unidoc/unihtml/client";_dce "github.com/unidoc/unihtml/content";_dc "github.com/unidoc/unihtml/sizes";_gg "github.com/unidoc/unipdf/v3/common/license";_dbf "github.com/unidoc/unipdf/v3/creator";_e "github.com/unidoc/unipdf/v3/model";_dg "net/url";_f "os";_fg "time";);func (_eb *Document )extract (_ge _c .Context ,_gf ,_ce _dc .Length ,_dfef margins )([]*_e .PdfPage ,error ){_cbg :=_g .BuildHTMLQuery ().SetContent (_eb ._df ).PageSize (_eb ._ec ).PaperWidth (_gf ).PaperHeight (_ce ).Orientation (_eb ._bda ).MarginLeft (_dfef .Left ).MarginRight (_dfef .Right ).MarginTop (_dfef .Top ).MarginBottom (_dfef .Bottom );_faa ,_fac :=_cbg .Query ();if _fac !=nil {return nil ,_fac ;};_ge ,_cbd :=_c .WithTimeout (_ge ,_fg .Second *15);defer _cbd ();_bfa ,_fac :=_ea .ConvertHTML (_ge ,_faa );if _fac !=nil {return nil ,_fac ;};_ffa :=_b .NewReader (_bfa .Data );_ebc ,_fac :=_e .NewPdfReader (_ffa );if _fac !=nil {return nil ,_fac ;};return _ebc .PageList ,nil ;};
|
|
|
|
// SetLandscapeOrientation sets document landscape page orientation.
|
|
func (_beb *Document )SetLandscapeOrientation (){_beb ._bda =_dc .Landscape };
|
|
|
|
// GeneratePageBlocks implements creator.Drawable interface.
|
|
func (_be *Document )GeneratePageBlocks (ctx _dbf .DrawContext )([]*_dbf .Block ,_dbf .DrawContext ,error ){if _dgg :=_be .validate ();_dgg !=nil {return nil ,ctx ,_dgg ;};var _ad []*_dbf .Block ;_cf :=_be .getMargins ();_eg ,_de :=_be ._dfc ,_be ._dd ;if _be ._dcc .IsRelative (){_eg ,_de =_dc .Point (ctx .Width ),_dc .Point (ctx .Height );};_bed ,_bf :=_be .extract (_c .Background (),_eg ,_de ,_cf );if _bf !=nil {return nil ,_dbf .DrawContext {},_bf ;};for _cdc ,_ffbe :=range _bed {_ecd ,_ae :=_dbf .NewBlockFromPage (_ffbe );if _ae !=nil {return nil ,_dbf .DrawContext {},_ae ;};_acf ,_acb ,_ae :=_ecd .GeneratePageBlocks (ctx );if _ae !=nil {return nil ,_dbf .DrawContext {},_ae ;};ctx =_acb ;if _cdc !=len (_bed )-1&&ctx .Y > (ctx .PageHeight -ctx .Margins .Bottom )*.95{ctx .X =ctx .Margins .Left ;ctx .Y =ctx .Margins .Top ;ctx .Page ++;};_ad =append (_ad ,_acf ...);};return _ad ,ctx ,nil ;};var ErrNoClient =_fc .New ("\u0055n\u0069\u0048\u0054\u004d\u004c\u0020\u0063\u006c\u0069\u0065\u006et\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");func (_gc *Document )getMargins ()margins {_cde :=_gc ._bg ;if _gc ._dcc .IsRelative (){_cde .Top =_dc .Millimeter (0);_cde .Left =_dc .Millimeter (0);_cde .Bottom =_dc .Millimeter (0);_cde .Right =_dc .Millimeter (0);return _cde ;};if _cde .Top ==nil {_cde .Top =_dc .Millimeter (10);};if _cde .Bottom ==nil {_cde .Bottom =_dc .Millimeter (10);};if _cde .Left ==nil {_cde .Left =_dc .Millimeter (10);};if _cde .Right ==nil {_cde .Right =_dc .Millimeter (10);};return _cde ;};
|
|
|
|
// GetPdfPages is a function that converts provided input content and
|
|
func (_dde *Document )GetPdfPages (ctx _c .Context )([]*_e .PdfPage ,error ){if _cd :=_dde .validate ();_cd !=nil {return nil ,_cd ;};return _dde .extract (ctx ,_dde ._dfc ,_dde ._dd ,_dde .getMargins ());};type margins struct{Left ,Right ,Bottom ,Top _dc .Length ;};
|
|
|
|
// SetMargins sets the Document Margins.
|
|
func (_bfc *Document )SetMargins (left ,right ,top ,bottom float64 ){_bfc ._bg .Left =_dc .Point (left );_bfc ._bg .Right =_dc .Point (right );_bfc ._bg .Top =_dc .Point (top );_bfc ._bg .Bottom =_dc .Point (bottom );_bfc ._dcc =_dbf .PositionAbsolute ;};
|
|
|
|
// Document is HTML document wrapper that is used for extracting and converting HTML document into PDF pages.
|
|
type Document struct{_df _dce .Content ;_bg margins ;_dcc _dbf .Positioning ;_ac ,_ff float64 ;_ec _dc .PageSize ;_dfc ,_dd _dc .Length ;_bda _dc .Orientation ;};
|
|
|
|
// SetMarginLeft sets the left margin.
|
|
func (_fb *Document )SetMarginLeft (margin _dc .Length ){_fb ._bg .Left =margin };
|
|
|
|
// SetPos sets the absolute position. Changes object positioning to absolute.
|
|
func (_ab *Document )SetPos (x ,y float64 ){_ab ._dcc =_dbf .PositionAbsolute ;_ab ._ac =x ;_ab ._ff =y };
|
|
|
|
// SetPageWidth sets the page width for given document.
|
|
func (_cb *Document )SetPageWidth (pageWidth _dc .Length )error {_cb ._dfc =pageWidth ;_cb ._dcc =_dbf .PositionAbsolute ;return nil ;};
|
|
|
|
// SetMarginBottom sets the left margin.
|
|
func (_egf *Document )SetMarginBottom (margin _dc .Length ){_egf ._bg .Bottom =margin };func _aa ()error {_fa :=_gg .GetLicenseKey ();if _fa ==nil {return _fc .New ("\u006e\u006f\u0020\u006cic\u0065\u006e\u0073\u0065\u0020\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064");};if !_fa .IsLicensed (){return _fc .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006f\u0072 \u006e\u006f\u0020\u006c\u0069\u0063\u0065n\u0073\u0065\u0020\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064");};if !_fa .UniHTML {return _fc .New ("\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064\u0020l\u0069\u0063e\u006e\u0073\u0065\u0020\u0069\u0073 \u006e\u006f\u0074\u0020\u0076\u0061\u006c\u0069d\u0020\u0066\u006f\u0072\u0020\u0074\u0068\u0065\u0020U\u006e\u0069\u0048\u0054\u004d\u004c\u0020\u0065\u0078\u0074\u0065n\u0073\u0069\u006f\u006e.\u0020\u0056i\u0073i\u0074\u003a\u0020\u0068\u0074\u0074\u0070s\u003a/\u002f\u0077\u0077\u0077\u002e\u0075\u006e\u0069\u0064\u006f\u0063\u002e\u0069\u006f\u0020\u0061\u006ed\u0020c\u006f\u006e\u0074\u0061\u0063\u0074\u0020\u0073\u0075\u0070\u0070\u006f\u0072t");};return nil ;};
|
|
|
|
// ChapterDraw implements creator.containerElement interface.
|
|
func (_dfec *Document )ContainerComponent (container _dbf .Drawable )(_dbf .Drawable ,error ){switch container .(type ){case *_dbf .Chapter :default:return nil ,_db .Errorf ("\u0075\u006e\u0069\u0068t\u006d\u006c\u002e\u0044\u006f\u0063\u0075\u006d\u0065n\u0074\u0020\u0063\u0061\u006e\u0027\u0074\u0020\u0062\u0065\u0020\u0061\u0020\u0063\u006f\u006d\u0070\u006fn\u0065\u006e\u0074\u0020\u006ff\u0020\u0074\u0068\u0065\u0020\u0025\u0054\u0020\u0063\u006f\u006e\u0074\u0061\u0069\u006e\u0065\u0072",container );};return _dfec ,nil ;};
|
|
|
|
// ConnectOptions creates UniHTML HTTP Client and tries to establish connection with the server.
|
|
func ConnectOptions (o Options )error {_ea =_g .New (_g .Options {Hostname :o .Hostname ,Port :o .Port ,HTTPS :o .Secure });_bc ,_a :=_c .WithTimeout (_c .Background (),_fg .Second *5);defer _a ();if _bd :=_ea .HealthCheck (_bc );_bd !=nil {return _bd ;};return nil ;};
|
|
|
|
// Connect creates UniHTML HTTP Client and tries to establish connection with the server.
|
|
func Connect (path string )error {if _fd :=_aa ();_fd !=nil {return _fd ;};_fcg ,_fe :=_g .ParseOptions (path );if _fe !=nil {return _fe ;};_ea =_g .New (_fcg );_ee ,_eae :=_c .WithTimeout (_c .Background (),_fg .Second *5);defer _eae ();if _ba :=_ea .HealthCheck (_ee );_ba !=nil {return _ba ;};return nil ;};var ErrContentNotDefined =_fc .New ("\u0068\u0074\u006d\u006c\u0020\u0064o\u0063\u0075\u006d\u0065\u006e\u0074\u0020\u0063\u006f\u006e\u0074\u0065\u006et\u0020\u006e\u006f\u0074\u0020\u0064\u0065f\u0069\u006e\u0065\u0064");
|
|
|
|
// SetMarginTop sets the left margin.
|
|
func (_cc *Document )SetMarginTop (margin _dc .Length ){_cc ._bg .Top =margin };var _ _dbf .Drawable =(*Document )(nil );
|
|
|
|
// SetMarginRight sets the left margin.
|
|
func (_bb *Document )SetMarginRight (margin _dc .Length ){_bb ._bg .Right =margin };
|
|
|
|
// SetPageSize sets the page default size.
|
|
func (_cfe *Document )SetPageSize (pageSize _dc .PageSize )error {if !pageSize .IsAPageSize (){return _fc .New ("\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064\u0020\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0070\u0061\u0067\u0065\u0020s\u0069\u007a\u0065");};_cfe ._ec =pageSize ;_cfe ._dcc =_dbf .PositionAbsolute ;return nil ;};
|
|
|
|
// NewDocument creates new HTML Document used as an input for the creator.Drawable.
|
|
func NewDocument (path string )(*Document ,error ){_dfe :=&Document {};_ef ,_fce :=_dg .Parse (path );if _fce !=nil {return nil ,_fce ;};switch _ef .Scheme {case "\u0068\u0074\u0074\u0070","\u0068\u0074\u0074p\u0073":_dfe ._df ,_fce =_dce .NewWebURL (path );if _fce !=nil {return nil ,_fce ;};return _dfe ,nil ;};_ffb ,_fce :=_f .Stat (path );if _fce !=nil {return nil ,_fce ;};if !_ffb .IsDir (){_dfe ._df ,_fce =_dce .NewHTMLFile (path );}else {_dfe ._df ,_fce =_dce .NewZipDirectory (path );};if _fce !=nil {return nil ,_fce ;};return _dfe ,nil ;};
|
|
|
|
// Options are the HTML Client options used for establishing the connection.
|
|
type Options struct{
|
|
|
|
// Hostname defines the hostname for the client. Default value: 'localhost'.
|
|
Hostname string ;
|
|
|
|
// Port defines the port at which the server works. Default value: '8080'
|
|
Port int ;
|
|
|
|
// Secure is the flag that states if the connection uses HTTPS protocol. Default Value: 'false'.
|
|
Secure bool ;
|
|
|
|
// Prefix is an option setting used when the server is working with the URI prefix. Default Value: ''.
|
|
Prefix string ;};func (_ed *Document )validate ()error {if _ea ==nil {return ErrNoClient ;};if _ed ._df ==nil {return ErrContentNotDefined ;};return nil ;};var _ea *_g .Client ;
|
|
|
|
// SetPageHeight sets the page height for given document.
|
|
func (_gd *Document )SetPageHeight (pageHeight _dc .Length )error {_gd ._dd =pageHeight ;_gd ._dcc =_dbf .PositionAbsolute ;return nil ;}; |